You are on page 1of 20

How to install Mongo DB on Windows :

 Download Mongo db by clicking on the link below


http://filehorse.com/download-mongodb/33269/
 The downloaded binary file has extension exe. Run it
 It will prompt for an installation wizard.
 Click Next
 Accept the licence agreement and click next
 Select complete installtion
 After installation click on finish
 Now, the mongodb is installed on the path C:/Program
Files/MongoDB/Server/3.4/bin.
 Bin directory contain several Binary File along with mongod and mongo.
To run it from other folder ,you could add path in system path. To do it :
1. Go to File Explorer and right click on This PC
2. Click on properties. Click on Advanced system setting on the left
pane.

3. Click on Environmental variables under the Advanced tab


4. Select Path from System variables section and click on Edit…..
5. Before Windows 10,append a semi-colon and paste the path given
above .From Windows 10,there is a New button to add new path
6. Click OKs to save changes
 Now , create a folder named data having a sub-folder named db
where you want to run the server. Mostly on the local disk C
 Start command prompt from there. Either changing the path in cmd
or clicking on open command window here which would be visible
after right clicking on the empty space inside the bin folder
 Write the command to start the server
 Mongod
It would start the server on port 27017 by default
 open another command prompt and type the following to start
client:
 mongo
 By default it connects to test database .If you see the line
connecting to: test. Then you have successfully installed mongodb.

SQL Terms MongoDB terms


Database Database
Table Collection
Entity/Row Document
Column Key/Field
Table Join Embeded Documents
Primary Key Primary Key (Default Key_id provided by
mongodb itself
Inserting two or more documents at a time
db.sailors.insertMany([

{sid:22,sname:'dustin',rating:7,age:45},{sid:29,sname:'brutus',rating:1,age:33},

{sid:31,sname:'lubber',rating:8,age:55},{sid:32,sname:'andy',rating:8,age:25.5},

{sid:58,sname:'rusty',rating:10,age:35},{sid:64,sname:'horatio',rating:7,age:35},

{sid:71,sname:'zorba',rating:7,age:16},{sid:74,sname:'horatio',rating:9,age:40},

{sid:85,sname:'art',rating:3,age:25.5},{sid:95,sname:'bob',rating:3,age:63.5}

])

-----Getting the inserted objects

db.sailors.find()

OUTPUT:

{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }

{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }

{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }

{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }

{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }

{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }

{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }

{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5 }

{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "dustin", "rating" : 7, "age" : 45 }

{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33 }


show dbs;

OUTPUT :

admin 0.000GB

db 0.000GB

local 0.000GB

test 0.000GB

#inserting one row

db.boats.insert({bid:101,bname:'interlake',color:'blue'});

#inserting many rows

db.boats.insertMany([

{bid:102,bname:'interlake',color:'red'},

{bid:103,bname:'clipper',color:'green'},

{bid:104,bname:'marine',color:'red'}

]);

# .pretty() would increase the readability

> db.boats.find().pretty();
{

"_id" : ObjectId("5d986aab44e2d81638842a9d"),

"bid" : 101,

"bname" : "interlake",

"color" : "blue"

"_id" : ObjectId("5d986b7244e2d81638842a9e"),

"bid" : 102,

"bname" : "interlake",

"color" : "red"

"_id" : ObjectId("5d986b7244e2d81638842a9f"),

"bid" : 103,

"bname" : "clipper",

"color" : "green"

"_id" : ObjectId("5d986b7244e2d81638842aa0"),

"bid" : 104,

"bname" : "marine",

"color" : "red"

}
#inserting rows

db.reserves.insertMany([

{sid:22,bid:102,day:'1998-10-10'},{sid:22,bid:103,day:'1998-10-8'},

{sid:22,bid:104,date:'1998-10-7'},{sid:31,bid:102,date:'1998-11-10'},

{sid:31,bid:103,date:'1998-11-6'},{sid:31,bid:104,date:'1998-11-12'},

{sid:64,sname:101,date:'1998-9-5'},{sid:64,bid:102,date:1998-9-8},

{sid:74,bid:103,date:'1998-9-8'}

]);

> db.reserves.find();

output

{ "_id" : ObjectId("5d986c5244e2d81638842aa1"), "sid" : 22, "bid" : 101, "day" : "1998-10-10" }

{ "_id" : ObjectId("5d986f0744e2d81638842aa2"), "sid" : 22, "bid" : 102, "day" : "1998-10-10" }

{ "_id" : ObjectId("5d986f0744e2d81638842aa3"), "sid" : 22, "bid" : 103, "day" : "1998-10-8" }

{ "_id" : ObjectId("5d986f0744e2d81638842aa4"), "sid" : 22, "bid" : 104, "date" : "1998-10-7" }

{ "_id" : ObjectId("5d986f0744e2d81638842aa5"), "sid" : 31, "bid" : 102, "date" : "1998-11-10" }

{ "_id" : ObjectId("5d986f0744e2d81638842aa6"), "sid" : 31, "bid" : 103, "date" : "1998-11-6" }

{ "_id" : ObjectId("5d986f0744e2d81638842aa7"), "sid" : 31, "bid" : 104, "date" : "1998-11-12" }

{ "_id" : ObjectId("5d986f0744e2d81638842aa8"), "sid" : 64, "sname" : 101, "date" : "1998-9-5" }

{ "_id" : ObjectId("5d986f0744e2d81638842aa9"), "sid" : 64, "bid" : 102, "date" : 1981 }

{ "_id" : ObjectId("5d986f0744e2d81638842aaa"), "sid" : 74, "bid" : 103, "date" : "1998-9-8" }

----Getting the created collections like boats, sailors etc.


> show collections;

output

boats

reserves

sailors

#update the entire object

>db.sailors.update({sid:22},{sid:22,sname:'Dustin',rating:7,age:45});

output

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

#update the entire object using $set

>db.sailors.update({sid:22},{$set: {sname:'Dustin',rating:7,age:45}},{multi: true});

output

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

#updating the single document

>db.sailors.updateOne({sid:22},{$set: {sname:'Dustin',rating:7,age:45}});

#updating many documents using $set

db.sailors.updateMany({sid:22},{$set:{age:20}});

output

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }


#update $push--- creates a new coloumn

db.sailors.update({sid:95},{$push:{nickname:'boo'}});

output

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

#update $pop --- pops the values in the coloumn

#-1 is first value

db.sailors.update({sid:22},{$pop:{nickname:1}});

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : -1 })

#update $pull

#pulls the values out from the coloumn

db.sailors.update({sid:22},{$pull:{nickname:'dust'}});

output

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

# +1 is for last values in the row

db.sailors.update({sid:22},{$pop:{nickname:1}});

output

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })


db.reserves.findOne({sid:31});

output

"_id" : ObjectId("5d986f0744e2d81638842aa5"),

"sid" : 31,

"bid" : 102,

"date" : "1998-11-10"

> db.reserves.find({sid:31});

output

{ "_id" : ObjectId("5d986f0744e2d81638842aa5"), "sid" : 31, "bid" : 102, "date" : "1998-11-10" }

{ "_id" : ObjectId("5d986f0744e2d81638842aa6"), "sid" : 31, "bid" : 103, "date" : "1998-11-6" }

{ "_id" : ObjectId("5d986f0744e2d81638842aa7"), "sid" : 31, "bid" : 104, "date" : "1998-11-12" }

#syntax

>db.collection.find.sort({key: 1 or -1});

1 for assending order -1 for decending order

Example:

db.sailors.find().sort({sid:1});

{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29 }

{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33 }

{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }

{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }


{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }

{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }

{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }

{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }

{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }

{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5 }

#getting documents using sid in descending order

> db.sailors.find().sort({sid:-1});

output

{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5 }

{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }

{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }

{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }

{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }

{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }

{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }

{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }

{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33 }

{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29 }


#count() --- counts no of rows

db.sailors.find().count();

10

#logical operators $and $or $not

#and

>db.sailors.find({$and:[{sid:22},{rating:7}]}).pretty();

"_id" : ObjectId("5d9868d644e2d81638842a9b"),

"sid" : 22,

"sname" : "Dustin",

"rating" : 7,

"age" : 29,

"nickname" : [ ]
#$or

db.sailors.find({$or:[{sid:22},{rating:7}]}).pretty();

"_id" : ObjectId("5d98671644e2d81638842a96"),

"sid" : 64,

"sname" : "horatio",

"rating" : 7,

"age" : 35

"_id" : ObjectId("5d98671644e2d81638842a97"),

"sid" : 71,

"sname" : "zorba",

"rating" : 7,

"age" : 16

"_id" : ObjectId("5d9868d644e2d81638842a9b"),

"sid" : 22,

"sname" : "Dustin",

"rating" : 7,

"age" : 29,

"nickname" : [ ]

}
#not

#skip #here it skips first 2 rows

db.sailors.find().skip(2);

{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }

{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }

{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }

{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }

{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }

{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5,


"nickname" : [ "boo" ] }

{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,


"nickname" : [ ] }

{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33,


"nickname" : [ "bru", "bru" ] }

>
db.sailors.find().count();

10

> db.boats.find().count();

# limit #shows only 2 rows

db.sailors.find().limit(2);

{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }

{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }

>

# condition operations

#$eq, $gt,$lt,$gte,$lte

db.sailors.find({sid:{$eq:22}});
{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,
"nickname" : [ ] }

>

#$lt--less than

db.sailors.find({rating:{$lt:7}});

{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }

{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5,


"nickname" : [ "boo" ] }

{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33,


"nickname" : [ "bru", "bru" ] }

#$gt-- greater than

> db.sailors.find({rating:{$gt:7}});

{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }

{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }

{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }

{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }

#$gte-- greater than eqaul to

> db.sailors.find({rating:{$gte:7}});

{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }

{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }


{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }

{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }

{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }

{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }

{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,


"nickname" : [ ] }

#$lte--- less than equal to

> db.sailors.find({rating:{$lte:7}});

{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }

{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }

{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }

{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5,


"nickname" : [ "boo" ] }

{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,


"nickname" : [ ] }

{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33,


"nickname" : [ "bru", "bru" ] }

>

#deletion

#deleteOne()

> db.sailors.deleteOne({rating:7});
{ "acknowledged" : true, "deletedCount" : 1 }

#deleteMany()

db.sailors.deleteMany({rating:7});

{ "acknowledged" : true, "deletedCount" : 2 }

#remove()

db.sailors.remove({sid:95});

WriteResult({ "nRemoved" : 1 })
#update $push--- creates a new coloumn
db.sailors.update({sid:95},{$push:{nickname:'boo'}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

 db.sailors.find();
{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }
{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }
{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }
{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5,
"nickname" : [ "boo" ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,
"nickname" : ["dust" ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33,
"nickname" : [ "bru", "bru" ] }
>

#update $pop --- pops the values in the coloumn


#+1 is first value
db.sailors.update({sid:22},{$pop:{nickname:1}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.sailors.find();
{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }
{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }
{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }
{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5,
"nickname" : [ "boo" ]  }
{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,
"nickname" : [ ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33,
"nickname" : [ "bru", "bru" ] }

#update $pull
#pulls the values out from the coloumn
 db.sailors.update({sid:22},{$pull:{nickname:'dust'}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.sailors.find();
{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }
{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }
{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }
{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5,
"nickname" : [ "boo" ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,
"nickname" : [ ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33,
"nickname" : [ "bru", "bru" ] }
>

# -1 is for last values in the row


db.sailors.update({sid:22},{$pop:{nickname:-1}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.sailors.find();
{ "_id" : ObjectId("5d98671644e2d81638842a93"), "sid" : 31, "sname" : "lubber", "rating" : 8, "age" : 55 }
{ "_id" : ObjectId("5d98671644e2d81638842a94"), "sid" : 32, "sname" : "andy", "rating" : 8, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a95"), "sid" : 58, "sname" : "rusty", "rating" : 10, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a96"), "sid" : 64, "sname" : "horatio", "rating" : 7, "age" : 35 }
{ "_id" : ObjectId("5d98671644e2d81638842a97"), "sid" : 71, "sname" : "zorba", "rating" : 7, "age" : 16 }
{ "_id" : ObjectId("5d98671644e2d81638842a98"), "sid" : 74, "sname" : "horatio", "rating" : 9, "age" : 40 }
{ "_id" : ObjectId("5d98671644e2d81638842a99"), "sid" : 85, "sname" : "art", "rating" : 3, "age" : 25.5 }
{ "_id" : ObjectId("5d98671644e2d81638842a9a"), "sid" : 95, "sname" : "bob", "rating" : 3, "age" : 63.5,
"nickname" : [  ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9b"), "sid" : 22, "sname" : "Dustin", "rating" : 7, "age" : 29,
"nickname" : [ ] }
{ "_id" : ObjectId("5d9868d644e2d81638842a9c"), "sid" : 29, "sname" : "brutus", "rating" : 1, "age" : 33,
"nickname" : [ "bru", "bru" ] }

You might also like