MongoDB Commands that need to know each who want to work with it
Essential MongoDB Commands Everyone Should Learn
There are some important commands with there example, i wish you try to implement them
List of all Databases
show databases
show dbs
Use DB or Create one if not exist
use db-name
Note :- in mongoDB DB can’t created until you can't store data in it.
View list of all collections in a DB
show collections
Command for create a collection
db.createCollection('collection-name')
Command for delete a collection
db.collection-name.drop()
Command for Delete a database
db.dropDatabase()
CRUD Operations
Create
Insert Single Document in a Collection
before continue, we need some sample data
Single Document Sample data
{
"maker": "Boeing",
"model": "737 Max",
"fuel_type": "Jet Fuel",
"engine": {
"type": "Turbofan",
"thrust": "23,000 lbs",
"manufacturer": "CFM International"
},
"capacity": {
"passengers": 200,
"crew": 6
},
"features": [
"In-flight Entertainment",
"Wi-Fi Connectivity",
"Advanced Navigation Systems"
],
"wingspan": "35.9 meters",
"cruise_speed": "839 km/h",
"max_range": "6,570 km",
"emergency_exit_doors": 4
}
let's insert it using the following commad
db.collection-name.insertOne(....)
use vehicles
db.createCollection.planes
db.planes.insertOne(
{
"maker": "Boeing",
"model": "737 Max",
"fuel_type": "Jet Fuel",
"engine": {
"type": "Turbofan",
"thrust": "23,000 lbs",
"manufacturer": "CFM International"
},
"capacity": {
"passengers": 200,
"crew": 6
},
"features": [
"In-flight Entertainment",
"Wi-Fi Connectivity",
"Advanced Navigation Systems"
],
"wingspan": "35.9 meters",
"cruise_speed": "839 km/h",
"max_range": "6,570 km",
"emergency_exit_doors": 4
}
)
ensure that you must received a acknowledged : true
Note : - when you inserting data in a collection that is not exist, created automatically, it's not necessary to create that collection manually.
Insert Many Documents at once in a Collection
Many Document [ {}, {}, {}] sample data
[
{
"maker": "Airbus",
"model": "A320neo",
"fuel_type": "Jet Fuel",
"engine": {
"type": "Turbofan",
"thrust": "27,000 lbs",
"manufacturer": "CFM International"
},
"capacity": {
"passengers": 180,
"crew": 6
},
"features": [
"Wi-Fi Connectivity",
"Advanced Avionics",
"Fuel Efficiency"
],
"wingspan": "35.8 meters",
"cruise_speed": "870 km/h",
"max_range": "6,300 km",
"emergency_exit_doors": 4
},
{
"maker": "Embraer",
"model": "E195-E2",
"fuel_type": "Jet Fuel",
"engine": {
"type": "Turbofan",
"thrust": "23,000 lbs",
"manufacturer": "Pratt & Whitney"
},
"capacity": {
"passengers": 132,
"crew": 4
},
"features": [
"In-flight Entertainment",
"Noise Reduction",
"Optimized Fuel Consumption"
],
"wingspan": "35.1 meters",
"cruise_speed": "870 km/h",
"max_range": "4,815 km",
"emergency_exit_doors": 2
},
{
"maker": "Bombardier",
"model": "CRJ900",
"fuel_type": "Jet Fuel",
"engine": {
"type": "Turbofan",
"thrust": "14,510 lbs",
"manufacturer": "General Electric"
},
"capacity": {
"passengers": 90,
"crew": 3
},
"features": [
"Comfortable Seating",
"Short-Range Efficiency",
"Compact Design"
],
"wingspan": "24.85 meters",
"cruise_speed": "830 km/h",
"max_range": "2,970 km",
"emergency_exit_doors": 2
},
{
"maker": "Cessna",
"model": "Citation Longitude",
"fuel_type": "Jet Fuel",
"engine": {
"type": "Turbofan",
"thrust": "7,665 lbs",
"manufacturer": "Honeywell"
},
"capacity": {
"passengers": 12,
"crew": 2
},
"features": [
"Luxurious Interiors",
"High-Speed Connectivity",
"Range Efficiency"
],
"wingspan": "21.0 meters",
"cruise_speed": "882 km/h",
"max_range": "6,482 km",
"emergency_exit_doors": 1
}
]
command for inset multiple documents at once
db.collection-name.insertMany(...)
db.planes.insertMany(
[
{
"maker": "Airbus",
"model": "A320neo",
"fuel_type": "Jet Fuel",
"engine": {
"type": "Turbofan",
"thrust": "27,000 lbs",
"manufacturer": "CFM International"
},
"capacity": {
"passengers": 180,
"crew": 6
},
"features": [
"Wi-Fi Connectivity",
"Advanced Avionics",
"Fuel Efficiency"
],
"wingspan": "35.8 meters",
"cruise_speed": "870 km/h",
"max_range": "6,300 km",
"emergency_exit_doors": 4
},
{
"maker": "Embraer",
"model": "E195-E2",
"fuel_type": "Jet Fuel",
"engine": {
"type": "Turbofan",
"thrust": "23,000 lbs",
"manufacturer": "Pratt & Whitney"
},
"capacity": {
"passengers": 132,
"crew": 4
},
"features": [
"In-flight Entertainment",
"Noise Reduction",
"Optimized Fuel Consumption"
],
"wingspan": "35.1 meters",
"cruise_speed": "870 km/h",
"max_range": "4,815 km",
"emergency_exit_doors": 2
},
{
"maker": "Bombardier",
"model": "CRJ900",
"fuel_type": "Jet Fuel",
"engine": {
"type": "Turbofan",
"thrust": "14,510 lbs",
"manufacturer": "General Electric"
},
"capacity": {
"passengers": 90,
"crew": 3
},
"features": [
"Comfortable Seating",
"Short-Range Efficiency",
"Compact Design"
],
"wingspan": "24.85 meters",
"cruise_speed": "830 km/h",
"max_range": "2,970 km",
"emergency_exit_doors": 2
},
{
"maker": "Cessna",
"model": "Citation Longitude",
"fuel_type": "Jet Fuel",
"engine": {
"type": "Turbofan",
"thrust": "7,665 lbs",
"manufacturer": "Honeywell"
},
"capacity": {
"passengers": 12,
"crew": 2
},
"features": [
"Luxurious Interiors",
"High-Speed Connectivity",
"Range Efficiency"
],
"wingspan": "21.0 meters",
"cruise_speed": "882 km/h",
"max_range": "6,482 km",
"emergency_exit_doors": 1
}
]
)
Read
find all data of a collection -find()
db.planes.find()
if it's not showing data in organized way you can use pretty
db.collection-name.find().pretty()
findOne()- give a data from the collection, that matched first
db.collection-name.findOne()
now if we need only specific values of a field, than use this command
db.collection-name.find({},{field-name : 1})
Don't forget to use and empty {}, and field-name : 1
,here 1 indicate true
examples -
by default it gives _id
, if you don’t want _id
field than add _id : 0
db.collection-name.find({},{field-name : 1, _id : 0})
So similarly if you need any field add field-name : 1
(inside of 1 or 0 you can also add true or false)
Read results for expected values
for example if you want to search all “737 Max model “
db.planes.find({model : "737 Max"})
you can use findOne ( remember it only return first matched result ), or any filter,
for example suppose i need a 737 Max model but inside whole data i just need maker and engine information
now if you think, how to find data based on subfield, mean if you want to find all maker with engine type Turbofan
- working on it , lets meet tomorrow