# MongoDB

MongoDB - The Complete Developer's Guide 2020

# How it works


how it works

MongoDB transforms the Json data into Bson (binary) in the server.


how it works


how it works

# MongoDB Ecosystem


how it works

# Installing MongoDB

  1. Download community version
  2. Create a folder in the root "c:data/db"
  3. Add MongoDB to the Path "C:\Program Files\MongoDB\Server\5.0\bin"
  4. Open bash as adm and Stop mongoDB server at windows with net stop MongoDB

To start the serve type mongod

Let the terminal running, open another one to work in it.


To start in a different folder mongod --dbpath "C:\Program......../data/db"

TIP

In windows MongoDB is a service, no need to start manually

# Starting with MongoDB

Start the Mongo shell with mongo then:

show dbs to show all the dbs.


Now connect to a database with use shop. It will connect even if the database doesn't exist.


Now create a selection with db.products.insertOne({name: "A Book", price: 12.99})

> db.products.insertOne({name: "A Book", price: 12.99})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("620cbb7edad777b2a42665b8")
}
1
2
3
4
5

Now we can have a loook at the database with db.products.find(), which gives us all the data of the collection.

> db.products.find()
{ "_id" : ObjectId("620cbb7edad777b2a42665b8"), "name" : "A Book", "price" : 12.99 }
1
2

We can also output as pretty like this: db.products.find().pretty()

{
        "_id" : ObjectId("620cbb7edad777b2a42665b8"),
        "name" : "A Book",
        "price" : 12.99
}
1
2
3
4
5

Adding a new product:

> db.products.insertOne({name: "Bible", price: "infinity", description: "Selfless subjugation of humanity"})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("620cbc92dad777b2a42665b9")
}
> db.products.find().pretty()                                                                               
{
        "_id" : ObjectId("620cbb7edad777b2a42665b8"),
        "name" : "A Book",
        "price" : 12.99
}
{
        "_id" : ObjectId("620cbc92dad777b2a42665b9"),
        "name" : "Bible",
        "price" : "infinity",
        "description" : "Selfless subjugation of humanity"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

and another one with nested details:

> db.products.insertOne({name: "Computer", price: 1299.99, description: "High quality computer", details: {cpu: "Intel i15 7889", memory: 64}}) 
{
        "acknowledged" : true,
        "insertedId" : ObjectId("620cbd96dad777b2a42665ba")
}
> db.products.find().pretty()                                                                                                                   
{
        "_id" : ObjectId("620cbb7edad777b2a42665b8"),
        "name" : "A Book",
        "price" : 12.99
}
{
        "_id" : ObjectId("620cbc92dad777b2a42665b9"),
        "name" : "Bible",
        "price" : "infinity",
        "description" : "Selfless subjugation of humanity"
}
{
        "_id" : ObjectId("620cbd96dad777b2a42665ba"),
        "name" : "Computer",
        "price" : 1299.99,
        "description" : "High quality computer",
        "details" : {
                "cpu" : "Intel i15 7889",
                "memory" : 64
        }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# Using MongoDB with other languages out of the Shell

MongoDB Drivers

# Working with MongoDB


Working with MongoDB


Working with MongoDB

# Course Outline


Working with MongoDB

# CRUD Basics

# Databases, Collections, and Documents


CRUD

To start the Shell we first start the server with mongod then, in another terminal we start the shell mongo.

To use a diferent port other than 27017, we just need to enter mongod --port 27018 and mongo --port 27018

# The Shell & MongoDB Drivers for Different Languages

MongoDB CRUD Operations

Tutorial for mongocxx

# Creating Databases & Collections

show bds to show the dbs we have.

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
1
2
3
4

We can switch to a database, even if it doesn't exist with use. But the database only gets created after we start entering data in it.

Now let's enter a key-value par.

> use flights                                                                                                                                
switched to db flights
> db.flightData.insertOne({departureAirport: "MUC", arrivalAirport: "SFO", aircraft: "Airbus A380", distance: 12000, intercontinental: true})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("620d958f77adc700e92368b6")
}
> db.flightData.insertOne({departureAirport: "LHR", arrivalAirport: "TXL", aircraft: "Airbus A320", distance: 950, intercontinental: false}) 
{
        "acknowledged" : true,
        "insertedId" : ObjectId("620d959277adc700e92368b7")
}
> show dbs                                                                                                                                   
admin    0.000GB
config   0.000GB
flights  0.000GB
local    0.000GB
test     0.000GB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

To see the data we can use db.flightData.find().pretty()

> db.flightData.find().pretty()
{
        "_id" : ObjectId("620d958f77adc700e92368b6"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true
}
{
        "_id" : ObjectId("620d959277adc700e92368b7"),
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

You can also define your own id with

db.flightData.insertOne({departureAirport: "LHR", arrivalAirport: "TXL", aircraft: "Airbus A320", distance: 950, intercontinental: false, _id: "txl-lhr-1})

> db.flightData.find().pretty()                                                                                                                               
{
        "_id" : ObjectId("620d958f77adc700e92368b6"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true
}
{
        "_id" : ObjectId("620d959277adc700e92368b7"),
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false
}
{
        "_id" : "txl-lhr-1",
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# Create, Read, Update, Delete (CRUD) & MongoDB


CRUD


CRUD

# Finding, Inserting, Deleting & Updating Elements:

A filter is defined as a document, therefore with {}. To deleteONe we define which key and value to delete inside a filter.

This will delete the first/ document with the arrivalAirport: "TXL"

> db.flightData.deleteOne({arrivalAirport: "TXL"})  
{ "acknowledged" : true, "deletedCount" : 1 }
> db.flightData.find().pretty()                     
{
        "_id" : ObjectId("620d958f77adc700e92368b6"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true
}
{
        "_id" : "txl-lhr-1",
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Now let's update one document so they have something in common for us to delete.

db.flightData.updateOne receives a filter {distance: 12000} which should find the first entry and the change to make, $set: {marker: "delete"}.

TIP

$ in mongoDB is reserved operator or word

$set is used on the updateOne operation to describe the change you want to make.

> db.flightData.updateOne({distance: 12000}, {$set:{marker: "delete"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.flightData.find().pretty()                                        
{
        "_id" : ObjectId("620d958f77adc700e92368b6"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true,
        "marker" : "delete"
}
{
        "_id" : "txl-lhr-1",
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

To add to all documents use updateMany.

> db.flightData.updateMany({}, {$set: {marker: "toDelete"}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
> db.flightData.find().pretty()                             
{
        "_id" : ObjectId("620d958f77adc700e92368b6"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true,
        "marker" : "toDelete"
}
{
        "_id" : "txl-lhr-1",
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false,
        "marker" : "toDelete"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

To delete all we can > db.flightData.deleteMany({marker: "toDelete"}) { "acknowledged" : true, "deletedCount" : 2 }

# Understanding 'insertMany()'

> db.flightData.insertMany([
...     {
...     "departureAirport" : "MUC",
...     "arrivalAirport" : "SFO",
...     "aircraft" : "Airbus A380",
...     "distance" : 12000,
...     "intercontinental" : true,
...     },
...     {
...     "departureAirport" : "LHR",
...     "arrivalAirport" : "TXL",
...     "aircraft" : "Airbus A320",
...     "distance" : 950,
...     "intercontinental" : false,
...     }
... ])
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("620de408049e17352da1de79"),
                ObjectId("620de408049e17352da1de7a")
        ]
}
> db.flightData.find().pretty()                             
{                              
        "_id" : ObjectId("620de408049e17352da1de79"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true
}
{
        "_id" : ObjectId("620de408049e17352da1de7a"),
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# Diving Deeper Into Finding Data

> db.flightData.find({intercontinental: true})
{ "_id" : ObjectId("620de408049e17352da1de79"), "departureAirport" : "MUC", "arrivalAirport" : "SFO", "aircraft" : "Airbus A380", "distance" : 12000, "intercontinental" : true }
> db.flightData.find({intercontinental: true}).pretty()
{
        "_id" : ObjectId("620de408049e17352da1de79"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true
}
1
2
3
4
5
6
7
8
9
10
11

Finding greater than 10000, $gt.

> db.flightData.find({distance:{$gt: 10000}}).pretty() 
{
        "_id" : ObjectId("620de408049e17352da1de79"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true
}
1
2
3
4
5
6
7
8
9

findOne will find the first and pretty() doesn't apply.

> db.flightData.findOne({distance:{$gt: 900}})         
{
        "_id" : ObjectId("620de408049e17352da1de79"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true
}
1
2
3
4
5
6
7
8
9

# 'update' vs 'updateMany()'

First, let's set delayed to true with updateOne

> db.flightData.updateOne({_id: ObjectId("620de408049e17352da1de79")}, {$set: {delayed: true}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.flightData.find().pretty()                                                                
{
        "_id" : ObjectId("620de408049e17352da1de79"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true,
        "delayed" : true
}
{
        "_id" : ObjectId("620de408049e17352da1de7a"),
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Just update works a bit like updateMany, as they update all matching elements. The difference can be seen if we take the $set like so:

> db.flightData.updateOne({_id: ObjectId("620de408049e17352da1de79")}, {delayed: false})        
uncaught exception: Error: the update operation document must contain atomic operators :
DBCollection.prototype.updateOne@src/mongo/shell/crud_api.js:565:19
@(shell):1:1
> db.flightData.updateMany({_id: ObjectId("620de408049e17352da1de79")}, {delayed: false})
uncaught exception: Error: the update operation document must contain atomic operators :
DBCollection.prototype.updateMany@src/mongo/shell/crud_api.js:655:19
@(shell):1:1
> db.flightData.update({_id: ObjectId("620de408049e17352da1de79")}, {delayed: false})    
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.flightData.find().pretty()
{ "_id" : ObjectId("620de408049e17352da1de79"), "delayed" : false }
{
"_id" : ObjectId("620de408049e17352da1de7a"),
"departureAirport" : "LHR",
"arrivalAirport" : "TXL",
"aircraft" : "Airbus A320",
"distance" : 950,
"intercontinental" : false
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Only update worked, but it overwrote all the other key-value pairs, it will only keep the`id.


If you really want to just rel=place the object a more explicity way to do it is with replaceOne

> db.flightData.replaceOne({_id: ObjectId("620de408049e17352da1de79")},{
...         "_id" : ObjectId("620de408049e17352da1de79"),               
...         "departureAirport" : "MUC",
...         "arrivalAirport" : "SFO",
...         "aircraft" : "Airbus A380",
...         "distance" : 12000,
...         "intercontinental" : true,
...         "delayed" : true
... })
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.flightData.find().pretty()                                                      
{                              
        "_id" : ObjectId("620de408049e17352da1de79"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true,
        "delayed" : true
}
{
        "_id" : ObjectId("620de408049e17352da1de7a"),
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# Understanding 'find()' & the Cursor Object

Insert a list of passengers:

> db.passengers.insertMany([         
...   {                              
...     "name": "Max Schwarzmueller",
...     "age": 29                    
...   },                             
...   {                              
...     "name": "Manu Lorenz",       
...     "age": 30                    
...   },                        
...   {                         
...     "name": "Chris Hayton", 
...     "age": 35               
...   },                        
...   {                         
...     "name": "Sandeep Kumar",
...     "age": 28               
...   },                          
...   {                           
...     "name": "Maria Jones",    
...     "age": 30                 
...   },                          
...   {                           
...     "name": "Alexandra Maier",
...     "age": 27                 
...   },                          
...   {                           
...     "name": "Dr. Phil Evans",
...     "age": 47
...   },
...   {
...     "name": "Sandra Brugge",
...     "age": 33
...   },
...   {
...     "name": "Elisabeth Mayr",
...     "age": 29
...   },
...   {
...     "name": "Frank Cube",
...     "age": 41
...   },
...   {
...     "name": "Karandeep Alun",
...     "age": 48
...   },
...   {
...     "name": "Michaela Drayer",
...     "age": 39
...   },
...   {
...     "name": "Bernd Hoftstadt",
...     "age": 22
...   },
...   {
...     "name": "Scott Tolib",
...     "age": 44
...   },
...   {
...     "name": "Freddy Melver",
...     "age": 41
...   },
...   {
...     "name": "Alexis Bohed",
...     "age": 35
...   },
...   {
...     "name": "Melanie Palace",
...     "age": 27
...   },
...   {
...     "name": "Armin Glutch",
...     "age": 35
...   },
...   {
...     "name": "Klaus Arber",
...     "age": 53
...   },
...   {
...     "name": "Albert Twostone",
...     "age": 68
...   },
...   {
...     "name": "Gordon Black",
...     "age": 38
...   }
... ]
... )
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("620ec392049e17352da1de7b"),
                ObjectId("620ec392049e17352da1de7c"),
                ObjectId("620ec392049e17352da1de7d"),
                ObjectId("620ec392049e17352da1de7e"),
                ObjectId("620ec392049e17352da1de7f"),
                ObjectId("620ec392049e17352da1de80"),
                ObjectId("620ec392049e17352da1de81"),
                ObjectId("620ec392049e17352da1de82"),
                ObjectId("620ec392049e17352da1de83"),
                ObjectId("620ec392049e17352da1de84"),
                ObjectId("620ec392049e17352da1de85"),
                ObjectId("620ec392049e17352da1de86"),
                ObjectId("620ec392049e17352da1de87"),
                ObjectId("620ec392049e17352da1de88"),
                ObjectId("620ec392049e17352da1de89"),
                ObjectId("620ec392049e17352da1de8a"),
                ObjectId("620ec392049e17352da1de8b"),
                ObjectId("620ec392049e17352da1de8c"),
                ObjectId("620ec392049e17352da1de8d"),
                ObjectId("620ec392049e17352da1de8e"),
                ObjectId("620ec392049e17352da1de8f")
        ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

db.passengers.find() gives us a cursor object and not all the data. We will have to execute the it command as suggested by the shell.


CRUD

db.passengers.find().toArray() will exhaust the cursor. And we will see the last passenger Gordon.

> db.passengers.find().pretty()
{
        "_id" : ObjectId("620ec392049e17352da1de7b"),
        "name" : "Max Schwarzmueller",
        "age" : 29
}
{
        "_id" : ObjectId("620ec392049e17352da1de7c"),
        "name" : "Manu Lorenz",
        "age" : 30
}
{
        "_id" : ObjectId("620ec392049e17352da1de7d"),
        "name" : "Chris Hayton",
        "age" : 35
}
{
        "_id" : ObjectId("620ec392049e17352da1de7e"),
        "name" : "Sandeep Kumar",
        "age" : 28
}
{
        "_id" : ObjectId("620ec392049e17352da1de7f"),
        "name" : "Maria Jones",
        "age" : 30
}
{
        "_id" : ObjectId("620ec392049e17352da1de80"),
        "name" : "Alexandra Maier",
        "age" : 27
}
{
        "_id" : ObjectId("620ec392049e17352da1de81"),
        "name" : "Dr. Phil Evans",
        "age" : 47
}
{
        "_id" : ObjectId("620ec392049e17352da1de82"),
        "name" : "Sandra Brugge",
        "age" : 33
}
{
        "_id" : ObjectId("620ec392049e17352da1de83"),
        "name" : "Elisabeth Mayr",
        "age" : 29
}
{
        "_id" : ObjectId("620ec392049e17352da1de84"),
        "name" : "Frank Cube",
        "age" : 41
}
{
        "_id" : ObjectId("620ec392049e17352da1de85"),
        "name" : "Karandeep Alun",
        "age" : 48
}
{
        "_id" : ObjectId("620ec392049e17352da1de86"),
        "name" : "Michaela Drayer",
        "age" : 39
}
{
        "_id" : ObjectId("620ec392049e17352da1de87"),
        "name" : "Bernd Hoftstadt",
        "age" : 22
}
{
        "_id" : ObjectId("620ec392049e17352da1de88"),
        "name" : "Scott Tolib",
        "age" : 44
}
{
        "_id" : ObjectId("620ec392049e17352da1de89"),
        "name" : "Freddy Melver",
        "age" : 41
}
{
        "_id" : ObjectId("620ec392049e17352da1de8a"),
        "name" : "Alexis Bohed",
        "age" : 35
}
{
        "_id" : ObjectId("620ec392049e17352da1de8b"),
        "name" : "Melanie Palace",
        "age" : 27
}
{
        "_id" : ObjectId("620ec392049e17352da1de8c"),
        "name" : "Armin Glutch",
        "age" : 35
}
{
        "_id" : ObjectId("620ec392049e17352da1de8d"),
        "name" : "Klaus Arber",
        "age" : 53
}
{
        "_id" : ObjectId("620ec392049e17352da1de8e"),
        "name" : "Albert Twostone",
        "age" : 68
}
Type "it" for more
> db.passengers.find().toArray()
[
        {
                "_id" : ObjectId("620ec392049e17352da1de7b"),
                "name" : "Max Schwarzmueller",
                "age" : 29
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de7c"),
                "name" : "Manu Lorenz",
                "age" : 30
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de7d"),
                "name" : "Chris Hayton",
                "age" : 35
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de7e"),
                "name" : "Sandeep Kumar",
                "age" : 28
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de7f"),
                "name" : "Maria Jones",
                "age" : 30
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de80"),
                "name" : "Alexandra Maier",
                "age" : 27
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de81"),
                "name" : "Dr. Phil Evans",
                "age" : 47
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de82"),
                "name" : "Sandra Brugge",
                "age" : 33
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de83"),
                "name" : "Elisabeth Mayr",
                "age" : 29
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de84"),
                "name" : "Frank Cube",
                "age" : 41
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de85"),
                "name" : "Karandeep Alun",
                "age" : 48
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de86"),
                "name" : "Michaela Drayer",
                "age" : 39
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de87"),
                "name" : "Bernd Hoftstadt",
                "age" : 22
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de88"),
                "name" : "Scott Tolib",
                "age" : 44
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de89"),
                "name" : "Freddy Melver",
                "age" : 41
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de8a"),
                "name" : "Alexis Bohed",
                "age" : 35
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de8b"),
                "name" : "Melanie Palace",
                "age" : 27
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de8c"),
                "name" : "Armin Glutch",
                "age" : 35
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de8d"),
                "name" : "Klaus Arber",
                "age" : 53
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de8e"),
                "name" : "Albert Twostone",
                "age" : 68
        },
        {
                "_id" : ObjectId("620ec392049e17352da1de8f"),
                "name" : "Gordon Black",
                "age" : 38
        }
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

db.passengers.find().forEach() allows you to right some code to do something to every document to the database.

in javascript you can pass an arrow function like this: db.passengers.find().forEach((passengerData) => {printjson(passengerData)}), and we will also get all the documents.

> db.passengers.find().forEach((passengerData) => {printjson(passengerData)})
{
        "_id" : ObjectId("620ec392049e17352da1de7b"),
        "name" : "Max Schwarzmueller",
        "age" : 29
}
{
        "_id" : ObjectId("620ec392049e17352da1de7c"),
        "name" : "Manu Lorenz",
        "age" : 30
}
{
        "_id" : ObjectId("620ec392049e17352da1de7d"),
        "name" : "Chris Hayton",
        "age" : 35
}
{
        "_id" : ObjectId("620ec392049e17352da1de7e"),
        "name" : "Sandeep Kumar",
        "age" : 28
}
{
        "_id" : ObjectId("620ec392049e17352da1de7f"),
        "name" : "Maria Jones",
        "age" : 30
}
{
        "_id" : ObjectId("620ec392049e17352da1de80"),
        "name" : "Alexandra Maier",
        "age" : 27
}
{
        "_id" : ObjectId("620ec392049e17352da1de81"),
        "name" : "Dr. Phil Evans",
        "age" : 47
}
{
        "_id" : ObjectId("620ec392049e17352da1de82"),
        "name" : "Sandra Brugge",
        "age" : 33
}
{
        "_id" : ObjectId("620ec392049e17352da1de83"),
        "name" : "Elisabeth Mayr",
        "age" : 29
}
{
        "_id" : ObjectId("620ec392049e17352da1de84"),
        "name" : "Frank Cube",
        "age" : 41
}
{
        "_id" : ObjectId("620ec392049e17352da1de85"),
        "name" : "Karandeep Alun",
        "age" : 48
}
{
        "_id" : ObjectId("620ec392049e17352da1de86"),
        "name" : "Michaela Drayer",
        "age" : 39
}
{
        "_id" : ObjectId("620ec392049e17352da1de87"),
        "name" : "Bernd Hoftstadt",
        "age" : 22
}
{
        "_id" : ObjectId("620ec392049e17352da1de88"),
        "name" : "Scott Tolib",
        "age" : 44
}
{
        "_id" : ObjectId("620ec392049e17352da1de89"),
        "name" : "Freddy Melver",
        "age" : 41
}
{
        "_id" : ObjectId("620ec392049e17352da1de8a"),
        "name" : "Alexis Bohed",
        "age" : 35
}
{
        "_id" : ObjectId("620ec392049e17352da1de8b"),
        "name" : "Melanie Palace",
        "age" : 27
}
{
        "_id" : ObjectId("620ec392049e17352da1de8c"),
        "name" : "Armin Glutch",
        "age" : 35
}
{
        "_id" : ObjectId("620ec392049e17352da1de8d"),
        "name" : "Klaus Arber",
        "age" : 53
}
{
        "_id" : ObjectId("620ec392049e17352da1de8e"),
        "name" : "Albert Twostone",
        "age" : 68
}
{
        "_id" : ObjectId("620ec392049e17352da1de8f"),
        "name" : "Gordon Black",
        "age" : 38
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

# Understanding Projection


CRUD

In this case the first curly braces will select all, and the second is name: 1, the value of 1 means included in the data you are returning to me.

> db.passengers.find({}, {name: 1}).pretty()
{
        "_id" : ObjectId("620ec392049e17352da1de7b"),
        "name" : "Max Schwarzmueller"
}
{ "_id" : ObjectId("620ec392049e17352da1de7c"), "name" : "Manu Lorenz" }
{ "_id" : ObjectId("620ec392049e17352da1de7d"), "name" : "Chris Hayton" }
{ "_id" : ObjectId("620ec392049e17352da1de7e"), "name" : "Sandeep Kumar" }
{ "_id" : ObjectId("620ec392049e17352da1de7f"), "name" : "Maria Jones" }
{ "_id" : ObjectId("620ec392049e17352da1de80"), "name" : "Alexandra Maier" }
{ "_id" : ObjectId("620ec392049e17352da1de81"), "name" : "Dr. Phil Evans" }
{ "_id" : ObjectId("620ec392049e17352da1de82"), "name" : "Sandra Brugge" }
{ "_id" : ObjectId("620ec392049e17352da1de83"), "name" : "Elisabeth Mayr" }
{ "_id" : ObjectId("620ec392049e17352da1de84"), "name" : "Frank Cube" }
{ "_id" : ObjectId("620ec392049e17352da1de85"), "name" : "Karandeep Alun" }
{ "_id" : ObjectId("620ec392049e17352da1de86"), "name" : "Michaela Drayer" }
{ "_id" : ObjectId("620ec392049e17352da1de87"), "name" : "Bernd Hoftstadt" }
{ "_id" : ObjectId("620ec392049e17352da1de88"), "name" : "Scott Tolib" }
{ "_id" : ObjectId("620ec392049e17352da1de89"), "name" : "Freddy Melver" }
{ "_id" : ObjectId("620ec392049e17352da1de8a"), "name" : "Alexis Bohed" }
{ "_id" : ObjectId("620ec392049e17352da1de8b"), "name" : "Melanie Palace" }
{ "_id" : ObjectId("620ec392049e17352da1de8c"), "name" : "Armin Glutch" }
{ "_id" : ObjectId("620ec392049e17352da1de8d"), "name" : "Klaus Arber" }
{ "_id" : ObjectId("620ec392049e17352da1de8e"), "name" : "Albert Twostone" }
Type "it" for more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

The id is by default always included. To exclude it give it a value of 0.

> db.passengers.find({}, {name: 1, _id: 0}).pretty()
{ "name" : "Max Schwarzmueller" }
{ "name" : "Manu Lorenz" }
{ "name" : "Chris Hayton" }
{ "name" : "Sandeep Kumar" }
{ "name" : "Maria Jones" }
{ "name" : "Alexandra Maier" }
{ "name" : "Dr. Phil Evans" }
{ "name" : "Sandra Brugge" }
{ "name" : "Elisabeth Mayr" }
{ "name" : "Frank Cube" }
{ "name" : "Karandeep Alun" }
{ "name" : "Michaela Drayer" }
{ "name" : "Bernd Hoftstadt" }
{ "name" : "Scott Tolib" }
{ "name" : "Freddy Melver" }
{ "name" : "Alexis Bohed" }
{ "name" : "Melanie Palace" }
{ "name" : "Armin Glutch" }
{ "name" : "Klaus Arber" }
{ "name" : "Albert Twostone" }
Type "it" for more

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# Embedded Documents & Arrays - The Theory

You can nest your documents all in one overarching document in one collection.


CRUD


CRUD

# Working with Embedded Documents

These are embedded document or nested documents.

> db.flightData.updateMany({}, {$set: {status: {decription:"on-time", lastUpdated: "1 hour ago" }}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
> db.flightData.find().pretty()                                                                     
{
        "_id" : ObjectId("620de408049e17352da1de79"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true,
        "delayed" : true,
        "status" : {
                "decription" : "on-time",
                "lastUpdated" : "1 hour ago"
        }
}
{
        "_id" : ObjectId("620de408049e17352da1de7a"),
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false,
        "status" : {
                "decription" : "on-time",
                "lastUpdated" : "1 hour ago"
        }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
> db.flightData.updateMany({}, {$set: {status: {decription:"on-time", lastUpdated: "1 hour ago" , details: {responsible: "Thiago"}}}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
> db.flightData.find().pretty()                                                                                                       
{
        "_id" : ObjectId("620de408049e17352da1de79"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true,
        "delayed" : true,
        "status" : {
                "decription" : "on-time",
                "lastUpdated" : "1 hour ago",
                "details" : {
                        "responsible" : "Thiago"
                }
        }
}
{
        "_id" : ObjectId("620de408049e17352da1de7a"),
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false,
        "status" : {
                "decription" : "on-time",
                "lastUpdated" : "1 hour ago",
                "details" : {
                        "responsible" : "Thiago"
                }
        }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

# Working with Arrays

Let's update the db with strings not documents, using arrays and [].

> db.passengers.updateOne({name: "Albert Twostone"}, {$set: {hobbies: ["sports", "cooking"]}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.passengers.find().pretty()
{
"_id" : ObjectId("620ec392049e17352da1de7b"),
"name" : "Max Schwarzmueller",
"age" : 29
}
{
"_id" : ObjectId("620ec392049e17352da1de7c"),
"name" : "Manu Lorenz",
"age" : 30
}
{
"_id" : ObjectId("620ec392049e17352da1de7d"),
"name" : "Chris Hayton",
"age" : 35
}
{
"_id" : ObjectId("620ec392049e17352da1de7e"),
"name" : "Sandeep Kumar",
"age" : 28
}
{
"_id" : ObjectId("620ec392049e17352da1de7f"),
"name" : "Maria Jones",
"age" : 30
}
{
"_id" : ObjectId("620ec392049e17352da1de80"),
"name" : "Alexandra Maier",
"age" : 27
}
{
"_id" : ObjectId("620ec392049e17352da1de81"),
"name" : "Dr. Phil Evans",
"age" : 47
}
{
"_id" : ObjectId("620ec392049e17352da1de82"),
"name" : "Sandra Brugge",
"age" : 33
}
{
"_id" : ObjectId("620ec392049e17352da1de83"),
"name" : "Elisabeth Mayr",
"age" : 29
}
{
"_id" : ObjectId("620ec392049e17352da1de84"),
"name" : "Frank Cube",
"age" : 41
}
{
"_id" : ObjectId("620ec392049e17352da1de85"),
"name" : "Karandeep Alun",
"age" : 48
}
{
"_id" : ObjectId("620ec392049e17352da1de86"),
"name" : "Michaela Drayer",
"age" : 39
}
{
"_id" : ObjectId("620ec392049e17352da1de87"),
"name" : "Bernd Hoftstadt",
"age" : 22
}
{
"_id" : ObjectId("620ec392049e17352da1de88"),
"name" : "Scott Tolib",
"age" : 44
}
{
"_id" : ObjectId("620ec392049e17352da1de89"),
"name" : "Freddy Melver",
"age" : 41
}
{
"_id" : ObjectId("620ec392049e17352da1de8a"),
"name" : "Alexis Bohed",
"age" : 35
}
{
"_id" : ObjectId("620ec392049e17352da1de8b"),
"name" : "Melanie Palace",
"age" : 27
}
{
"_id" : ObjectId("620ec392049e17352da1de8c"),
"name" : "Armin Glutch",
"age" : 35
}
{
"_id" : ObjectId("620ec392049e17352da1de8d"),
"name" : "Klaus Arber",
"age" : 53
}
{
"_id" : ObjectId("620ec392049e17352da1de8e"),
"name" : "Albert Twostone",
"age" : 68,
"hobbies" : [
"sports",
"cooking"
]
}
Type "it" for more

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

# Accessing Structured Data

Let's Query an array first

> db.passengers.find({name: "Albert Twostone"}).pretty()
{
        "_id" : ObjectId("620ec392049e17352da1de8e"),
        "name" : "Albert Twostone",
        "age" : 68,
        "hobbies" : [
                "sports",
                "cooking"
        ]
}
> db.passengers.findOne({name: "Albert Twostone"}).hobbies
[ "sports", "cooking" ]
> db.passengers.find({hobbies: "sports"}).pretty()        
{
        "_id" : ObjectId("620ec392049e17352da1de8e"),
        "name" : "Albert Twostone",
        "age" : 68,
        "hobbies" : [
                "sports",
                "cooking"
        ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Now let's query objects.

When using a . to access the object you have to use "".

> db.flightData.find({"status.description": "on-time"}).pretty() 
{
        "_id" : ObjectId("620de408049e17352da1de79"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true,
        "delayed" : true,
        "status" : {
                "description" : "on-time",
                "lastUpdated" : "1 hour ago",
                "details" : {
                        "responsible" : "Thiago"
                }
        }
}
{
        "_id" : ObjectId("620de408049e17352da1de7a"),
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false,
        "status" : {
                "description" : "on-time",
                "lastUpdated" : "1 hour ago",
                "details" : {
                        "responsible" : "Thiago"
                }
        }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
> db.flightData.find({"status.details.responsible": "Thiago"}).pretty()
{
        "_id" : ObjectId("620de408049e17352da1de79"),
        "departureAirport" : "MUC",
        "arrivalAirport" : "SFO",
        "aircraft" : "Airbus A380",
        "distance" : 12000,
        "intercontinental" : true,
        "delayed" : true,
        "status" : {
                "decription" : "on-time",
                "lastUpdated" : "1 hour ago",
                "details" : {
                        "responsible" : "Thiago"
                }
        }
}
{
        "_id" : ObjectId("620de408049e17352da1de7a"),
        "departureAirport" : "LHR",
        "arrivalAirport" : "TXL",
        "aircraft" : "Airbus A320",
        "distance" : 950,
        "intercontinental" : false,
        "status" : {
                "decription" : "on-time",
                "lastUpdated" : "1 hour ago",
                "details" : {
                        "responsible" : "Thiago"
                }
        }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# Summary


CRUD

MongoDB Operators