MongoDB Cheat Sheet

show dbs
db // prints the current database

use <database_name>

show tables / show collections


CRUD operations:
==============================================================================
CRUD: create, read, update, delete,remove




select <projection> from Student where <condition>

db.Student.find(<condition>,<projeciton>)


db.products.insert({item:"TV",aqty:200,soldqty:1200,avail:true})

db.products.insert({item:"MP3",aqty:300,soldqty:500,avail:true})

db.products.insert({item:"MOBILE",aqty:0,soldqty:2000,avail:false})

db.products.insert({item:"MOUSE",aqty:1000,soldqty:3000,avail:true})

db.products.insert({item:"LAPTOP",aqty:0,soldqty:1800,avail:false})

db.products.insert({item:"AC",aqty:100,soldqty:700,avail:true})

db.products.insert({item:"KEYBOARD",aqty:2000,soldqty:500,avail:true})

db.products.insert({item:"SPEAKER",aqty:300,soldqty:3000,avail:true})

db.products.insert({item:"OVEN",aqty:500,soldqty:1200,avail:true})

db.products.insert({item:"FAN",aqty:550,soldqty:750,avail:true})

db.products.insert({item:"BULB",aqty:2000,soldqty:4000,avail:true})

================================================================================================================================
Reading all the data from collection
=================================================================================================================================
> db.products.find()
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
> db.products.find({},{})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
> db.products.find({})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }

==================================================================================================================================================
Reading the data with condtions and projections

IMP note: We can not read the data with projection without the condition
===============================================================================================================================================
> db.products.find()
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
> db.products.find({aqty:0})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>
>
>
> db.products.find({aqty:0},{item:1,soldqty:!})
2022-09-05T11:09:50.521+0530 E QUERY [js] uncaught exception: SyntaxError: expected expression, got '}' :
@(shell):1:43
>
>
> db.products.find({aqty:0},{item:1,soldqty:1})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "soldqty" : 2000 }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "soldqty" : 1800 }
>
>
> db.products.find({item:1,soldqty:1})
>
> db.products.find({},{item:1,soldqty:1})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "soldqty" : 1200 }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "soldqty" : 500 }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "soldqty" : 2000 }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "soldqty" : 3000 }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "soldqty" : 1800 }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "soldqty" : 700 }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "soldqty" : 500 }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "soldqty" : 3000 }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "soldqty" : 1200 }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "soldqty" : 750 }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "soldqty" : 4000 }

==========================================================================================================================================
Reading the data with limit(), sort(), skip() functions
========================================================================================================================================
> db.products.find()
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
> db.products.find().sort({item:1})
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
>
>
> db.products.find().sort({item:-1})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
>
>
> db.products.find().sort({aqy:1})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
> db.products.find().sort({aqty:1})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
> db.products.find().sort({aqty:1}).limit(5)
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
>
> db.products.find().sort({aqty:1}).skip()
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
> db.products.find().sort({aqty:1}).skip(5)
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }


=========================================================================================================================================================
To know the structure of the documents in a collection we findOne() function generally as a DBA
==========================================================================================================================================================
> db.products.findOne()
{
"_id" : ObjectId("631588c41db3f60a41ec1194"),
"item" : "TV",
"aqty" : 200,
"soldqty" : 1200,
"avail" : true
}

=====================================================================================================================================================
Reading the data with more readability we can use pretty() function
=====================================================================================================================================================
> db.products.find()
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
> db.products.find().pretty()
{
"_id" : ObjectId("631588c41db3f60a41ec1194"),
"item" : "TV",
"aqty" : 200,
"soldqty" : 1200,
"avail" : true
}
{
"_id" : ObjectId("631588c41db3f60a41ec1195"),
"item" : "MP3",
"aqty" : 300,
"soldqty" : 500,
"avail" : true
}
{
"_id" : ObjectId("631588c41db3f60a41ec1196"),
"item" : "MOBILE",
"aqty" : 0,
"soldqty" : 2000,
"avail" : false
}
{
"_id" : ObjectId("631588c41db3f60a41ec1197"),
"item" : "MOUSE",
"aqty" : 1000,
"soldqty" : 3000,
"avail" : true
}


==========================================================================================================================================
Reading the with equality condition
=========================================================================================================================================
> db.products.find()
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
> db.products.find({aqty:0})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>
>
> db.products.find({aqty:500})
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
>
> db.products.find({aqty:2000})
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
> db.products.find({item:"BULB"})
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
> db.products.find({avail:false})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>

========================================================================================================================================
Reading the data with in operator
============================================================================================================================================
> db.products.find()
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
>
> db.products.find({aqty:{$in:[0,200]}})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>
> db.products.find({aqty:{$in:[0,2000]}})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
> db.products.find({aqty:{$in:[0,2000,550]}})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }


=======================================================================================================================================================================
Reading the with AND operator:
=======================================================================================================================================================================

> db.products.find()
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
> db.products.find({aqty:200,soldqty:1500})
>
>
> db.products.find({aqty:0,soldqty:1800})
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>
>
> db.products.find({aqty:0,soldqty:1800})
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>
>
> db.products.find({aqty:{$lt:200},soldqty:1800})
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>
>
> db.products.find({aqty:{$lt:200}})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
>
> db.products.find({aqty:{$lt:200},soldqty:1800})
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>
>
> db.products.find({aqty:{$lt:200},soldqty:{$gt:1500}})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>

> db.products.find({aqty:{$gt:200},soldqty:{$gt:1500}})
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
> db.products.find({aqty:{$gte:200},soldqty:{$gt:1200}})
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
> db.products.find({aqty:{$gte:200},soldqty:{$gte:1200}})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }


====================================================================================================================================================
reading the data OR operator
====================================================================================================================================================

> db.products.find()
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
> db.products.find({$or:[{aqty:200},{soldqty:3000}]})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
>
>
>
> db.products.find({$or:[{aqty:{$lt:200}},{soldqty:{$gt:1800}}]})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
> db.products.find({$or:[{aqty:{$lte:200}},{soldqty:{$gte:1800}}]})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }





CLASS -2

===================================================================================================================================
Reading the data with OR and And operators
=========================================================================================================================================
> db.products.find()
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
> db.products.find({aqty:0})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>
>
> db.products.find({aqty:0},$or:[{aqty:{$gte:0}},{soldqty:{$lte:1500}}])
2022-09-07T10:36:57.413+0530 E QUERY [js] uncaught exception: SyntaxError: missing ) after argument list :
@(shell):1:29
>
>
> db.products.find({aqty:0})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>
>
> db.products.find({aqty:0,$or:[{aqty:{$gte:0}},{soldqty:{$lte:1500}}])
...
...
>
> db.products.find({aqty:0,$or:[{aqty:{$gte:0}},{soldqty:{$lte:1500}}]})
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>
>
>
> db.products.find({aqty:0,$or:[{aqty:{$gt:0}},{soldqty:{$lte:1500}}]})
>
>
>
> db.products.find({aqty:{$lte:200},$or:[{aqty:{$gt:0}},{soldqty:{$lte:1500}}]})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
>
>
>
> db.products.find({aqty:{$lte:200},$or:[{aqty:{$gte:0}},{soldqty:{$lte:1500}}]})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
>
>
>
>
> db.products.find()
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
>
> db.products.find({avail:True})
2022-09-07T10:42:46.175+0530 E QUERY [js] uncaught exception: ReferenceError: True is not defined :
@(shell):1:19
>
> db.products.find({avail:true})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
>
> db.products.find({avail:true,$or:[{aqty:0},{soldqty:1200}]})
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }


============================================================================================================================================================
Reading the data with regular expressions
==============================================================================================================================================================
> db.products.find()
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
>
>
>
> db.products.find({item:/^M/})
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
>
>
> db.products.find({item:/M/})
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
>
>
> db.products.find({item:/A/})
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
>
> db.products.find({item:/TOP/})
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
>
>
> db.products.find({item:/^TOP/})
>
>
> db.products.find({item:/TOP$/})
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }

> db.products.find()
{ "_id" : ObjectId("631588c41db3f60a41ec1194"), "item" : "TV", "aqty" : 200, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1198"), "item" : "LAPTOP", "aqty" : 0, "soldqty" : 1800, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1199"), "item" : "AC", "aqty" : 100, "soldqty" : 700, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119a"), "item" : "KEYBOARD", "aqty" : 2000, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119b"), "item" : "SPEAKER", "aqty" : 300, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119c"), "item" : "OVEN", "aqty" : 500, "soldqty" : 1200, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119d"), "item" : "FAN", "aqty" : 550, "soldqty" : 750, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec119e"), "item" : "BULB", "aqty" : 2000, "soldqty" : 4000, "avail" : true }
{ "_id" : ObjectId("63182a632040a5ace4df3699"), "item" : "mp3" }
>
>
> db.products.find({item:/^m/})
{ "_id" : ObjectId("63182a632040a5ace4df3699"), "item" : "mp3" }
>
> db.products.find({item:/^m/i})
{ "_id" : ObjectId("631588c41db3f60a41ec1195"), "item" : "MP3", "aqty" : 300, "soldqty" : 500, "avail" : true }
{ "_id" : ObjectId("631588c41db3f60a41ec1196"), "item" : "MOBILE", "aqty" : 0, "soldqty" : 2000, "avail" : false }
{ "_id" : ObjectId("631588c41db3f60a41ec1197"), "item" : "MOUSE", "aqty" : 1000, "soldqty" : 3000, "avail" : true }
{ "_id" : ObjectId("63182a632040a5ace4df3699"), "item" : "mp3" }
>

===================================================================================================================================
Reading the data from arrray fields
=================================================================================================================================
> db.colors.find()
{ "_id" : ObjectId("63182ad32040a5ace4df369a"), "color" : [ "red", "blue", "violet" ] }
{ "_id" : ObjectId("63182ae42040a5ace4df369b"), "color" : [ "yellow", "red", "violet" ] }
{ "_id" : ObjectId("63182af32040a5ace4df369c"), "color" : [ "white", "red", "black" ] }
{ "_id" : ObjectId("63182b072040a5ace4df369d"), "color" : [ "orange", "black", "green" ] }
{ "_id" : ObjectId("63182b1d2040a5ace4df369e"), "color" : [ "indigo", "blue", "brinjal" ] }
>
>
>
> db.colors.find({color:"black"})
{ "_id" : ObjectId("63182af32040a5ace4df369c"), "color" : [ "white", "red", "black" ] }
{ "_id" : ObjectId("63182b072040a5ace4df369d"), "color" : [ "orange", "black", "green" ] }
>
>
> db.colors.find({color:"red"})
{ "_id" : ObjectId("63182ad32040a5ace4df369a"), "color" : [ "red", "blue", "violet" ] }
{ "_id" : ObjectId("63182ae42040a5ace4df369b"), "color" : [ "yellow", "red", "violet" ] }
{ "_id" : ObjectId("63182af32040a5ace4df369c"), "color" : [ "white", "red", "black" ] }
>
>
> db.colors.find({color:"blue"})
{ "_id" : ObjectId("63182ad32040a5ace4df369a"), "color" : [ "red", "blue", "violet" ] }
{ "_id" : ObjectId("63182b1d2040a5ace4df369e"), "color" : [ "indigo", "blue", "brinjal" ] }
>
>
>
> db.colors.find({'color.0':"blue"})
>
>
> db.colors.find({'color.0':"red"})
{ "_id" : ObjectId("63182ad32040a5ace4df369a"), "color" : [ "red", "blue", "violet" ] }
>
>
> db.colors.find({'color.1':"red"})
{ "_id" : ObjectId("63182ae42040a5ace4df369b"), "color" : [ "yellow", "red", "violet" ] }
{ "_id" : ObjectId("63182af32040a5ace4df369c"), "color" : [ "white", "red", "black" ] }
>
>
> db.colors.find({'color.2':"violet"})
{ "_id" : ObjectId("63182ad32040a5ace4df369a"), "color" : [ "red", "blue", "violet" ] }
{ "_id" : ObjectId("63182ae42040a5ace4df369b"), "color" : [ "yellow", "red", "violet" ] }
>
>
>
>
> db.colors.find({'color.2':"violet"},{item:{$slice:2}})
{ "_id" : ObjectId("63182ad32040a5ace4df369a"), "color" : [ "red", "blue", "violet" ] }
{ "_id" : ObjectId("63182ae42040a5ace4df369b"), "color" : [ "yellow", "red", "violet" ] }
>
>
>
>
> db.colors.find({'color.2':"violet"},{item:{$slice:1}})
{ "_id" : ObjectId("63182ad32040a5ace4df369a"), "color" : [ "red", "blue", "violet" ] }
{ "_id" : ObjectId("63182ae42040a5ace4df369b"), "color" : [ "yellow", "red", "violet" ] }
>
>
>
>
> db.colors.find({'color.2':"violet"},{color:{$slice:1}})
{ "_id" : ObjectId("63182ad32040a5ace4df369a"), "color" : [ "red" ] }
{ "_id" : ObjectId("63182ae42040a5ace4df369b"), "color" : [ "yellow" ] }
>
>
>
>
> db.colors.find({},{color:{$slice:1}})
{ "_id" : ObjectId("63182ad32040a5ace4df369a"), "color" : [ "red" ] }
{ "_id" : ObjectId("63182ae42040a5ace4df369b"), "color" : [ "yellow" ] }
{ "_id" : ObjectId("63182af32040a5ace4df369c"), "color" : [ "white" ] }
{ "_id" : ObjectId("63182b072040a5ace4df369d"), "color" : [ "orange" ] }
{ "_id" : ObjectId("63182b1d2040a5ace4df369e"), "color" : [ "indigo" ] }
>
>
> db.colors.find({},{color:{$slice:2}})
{ "_id" : ObjectId("63182ad32040a5ace4df369a"), "color" : [ "red", "blue" ] }
{ "_id" : ObjectId("63182ae42040a5ace4df369b"), "color" : [ "yellow", "red" ] }
{ "_id" : ObjectId("63182af32040a5ace4df369c"), "color" : [ "white", "red" ] }
{ "_id" : ObjectId("63182b072040a5ace4df369d"), "color" : [ "orange", "black" ] }
{ "_id" : ObjectId("63182b1d2040a5ace4df369e"), "color" : [ "indigo", "blue" ] }
>
>
>
> db.colors.find({},{color:{$slice:3}})
{ "_id" : ObjectId("63182ad32040a5ace4df369a"), "color" : [ "red", "blue", "violet" ] }
{ "_id" : ObjectId("63182ae42040a5ace4df369b"), "color" : [ "yellow", "red", "violet" ] }
{ "_id" : ObjectId("63182af32040a5ace4df369c"), "color" : [ "white", "red", "black" ] }
{ "_id" : ObjectId("63182b072040a5ace4df369d"), "color" : [ "orange", "black", "green" ] }
{ "_id" : ObjectId("63182b1d2040a5ace4df369e"), "color" : [ "indigo", "blue", "brinjal" ] }
>

==================================================================================================================================================================================
update operations
=================================================================================================================================================================================
1)changing existing field values
2)Adding the extra fields
3)removing unneccessary fields
4)replacing the existing documents
5)incremeting and decrementing existing field values


===============================================================================================================================================================================
1)changing existing field values
===============================================================================================================================================================================

> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 1, "sname" : "Ravinder", "address" : "hyderabad" }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Prabhu", "address" : "hyderabad" }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Prabhu", "address" : "hyderabad" }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "kumar", "address" : "hyderabad" }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Bhusan", "address" : "pune" }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Bala", "address" : "pune" }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 3, "sname" : "Raja", "address" : "hyd" }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd" }
>
> db.students.update({sno:1},{$set:{sname:"Ravinder"}},{multi:true})
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 3 })
>
>
> db.students.insert({sno:6})
WriteResult({ "nInserted" : 1 })
>
>
> cls
>
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 1, "sname" : "Ravinder", "address" : "hyderabad" }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Ravinder", "address" : "hyderabad" }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Ravinder", "address" : "hyderabad" }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Ravinder", "address" : "hyderabad" }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Bhusan", "address" : "pune" }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Bala", "address" : "pune" }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 3, "sname" : "Raja", "address" : "hyd" }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd" }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6 }
>
>
> db.students.update({sno:2},{$set:{sname:"Balakrishna",address:"hyd"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
> db.students.update({sno:2},{$set:{sname:"Balakrishna",address:"hyd"}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 1 })
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 1, "sname" : "Ravinder", "address" : "hyderabad" }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Ravinder", "address" : "hyderabad" }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Ravinder", "address" : "hyderabad" }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Ravinder", "address" : "hyderabad" }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 3, "sname" : "Raja", "address" : "hyd" }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd" }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6 }

========================================================================================================================================================
2)Adding the extra fields
=======================================================================================================================================================
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 3, "sname" : "Raja", "address" : "hyd" }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd" }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6 }
>
>
> db.students.update({sno:6},{$set:{sname:"Ravi",address:"Manikonda"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 3, "sname" : "Raja", "address" : "hyd" }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd" }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi" }
>
>
> db.students.update({sno:6},{$set:{sname1:"Ravi",address1:"Manikonda"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 3, "sname" : "Raja", "address" : "hyd" }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd" }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "address1" : "Manikonda", "sname1" : "Ravi" }
>
>
>
>
> db.students.update({},{$set:{phno:2342342342}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
> db.students.update({},{$set:{phno:2342342342}},{multi:true})
WriteResult({ "nMatched" : 11, "nUpserted" : 0, "nModified" : 10 })
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 3, "sname" : "Raja", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "address1" : "Manikonda", "sname1" : "Ravi", "phno" : 2342342342 }


=================================================================================================================================================================================
3)Removing the unneccessary fields
=========================================================================================================================================================================
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 3, "sname" : "Raja", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "address1" : "Manikonda", "sname1" : "Ravi", "phno" : 2342342342 }
>
>
>
> db.students.update({sno:6},{$unset:{sname1:1,address1:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 3, "sname" : "Raja", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "phno" : 2342342342 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "phno" : 2342342342 }
>
>
>
> db.students.update({},{$unset:{phno:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.students.update({},{$unset:{phno:1}},{multi:true})
WriteResult({ "nMatched" : 11, "nUpserted" : 0, "nModified" : 10 })
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 3, "sname" : "Raja", "address" : "hyd" }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd" }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi" }

==============================================================================================================================================================
Replacing the existing the documents
=========================================================================================================================================================
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 3, "sname" : "Raja", "address" : "hyd" }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd" }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi" }
>
>
>
> db.students.update({sno:1},{sno:10,sname:"kumar",phno:132322})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 10, "sname" : "kumar", "phno" : 132322 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 3, "sname" : "Raja", "address" : "hyd" }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd" }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi" }
>
> db.students.update({sno:1},{sno:10,sname:"kumar",phno:132322},{multi:true})
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 9,
"errmsg" : "multi update is not supported for replacement-style update"
}
})
>
>
>
> db.students.update({sname:"Raja"},{sno:20,sname:"Naveen",phno:132322})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 10, "sname" : "kumar", "phno" : 132322 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sname" : "Naveen", "phno" : 132322 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd" }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi" }
>
>
> db.students.update({sname:"Raja"},{sno:20})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
>
>
>
>
> db.students.update({sname:"Naveen"},{sno:20})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sno" : 10, "sname" : "kumar", "phno" : 132322 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd" }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd" }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd" }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi" }

===================================================================================================================================================================================
Incrementing or decrementing existing filed values
========================================================================================================================================================================================
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sal" : 2000 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 6000 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 4000 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 4000 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 4000 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 4000 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sal" : 2000 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "sal" : 2000 }
>
>
> db.students.update({sname:"Balakrishna"},{$inc:{sal:2000}},{multi:true})
WriteResult({ "nMatched" : 5, "nUpserted" : 0, "nModified" : 5 })
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sal" : 2000 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 8000 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 6000 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 6000 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 6000 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 6000 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sal" : 2000 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "sal" : 2000 }
>
>
> db.students.update({sname:"Balakrishna"},{$inc:{sal:-2000}},{multi:true})
WriteResult({ "nMatched" : 5, "nUpserted" : 0, "nModified" : 5 })
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sal" : 2000 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 6000 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 4000 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 4000 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 4000 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 4000 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sal" : 2000 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "sal" : 2000 }
>
>
> db.students.update({sname:"Balakrishna"},{$inc:{sal:-2000}},{multi:true})
WriteResult({ "nMatched" : 5, "nUpserted" : 0, "nModified" : 5 })
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sal" : 2000 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 4000 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sal" : 2000 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "sal" : 2000 }
>
>
> db.students.update({sname:"Balakrishna"},{$inc:{sal:-2000}},{multi:true})
WriteResult({ "nMatched" : 5, "nUpserted" : 0, "nModified" : 5 })
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sal" : 2000 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sal" : 2000 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "sal" : 2000 }
>
====================================================================================================================================================================================
upsert: insert+ update

Note: upsert will update the record it if matches with condition, else it will insert the new record.
======================================================================================================================================================================================
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sal" : 2000 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sal" : 2000 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "sal" : 2000 }
>
>
>
>

>
> db.students.update({sno:7},{sno:10,sname:"pratap",address:"hyderabad"},{upsert:true})
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("631835354ee73d03009b58ec")
})
>
>
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sal" : 2000 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sal" : 2000 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "sal" : 2000 }
{ "_id" : ObjectId("631835354ee73d03009b58ec"), "sno" : 10, "sname" : "pratap", "address" : "hyderabad" }
>
>
>
> db.students.update({sno:7},{sno:10,sname:"pratap",address:"hyderabad"},{upsert:true})
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("6318355d4ee73d03009b58f8")
})
>
>
> db.students.update({sno:7},{sno:10,sname:"pratap",address:"hyderabad"},{upsert:true})
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("6318355e4ee73d03009b58fd")
})
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sal" : 2000 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sal" : 2000 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "sal" : 2000 }
{ "_id" : ObjectId("631835354ee73d03009b58ec"), "sno" : 10, "sname" : "pratap", "address" : "hyderabad" }
{ "_id" : ObjectId("6318355d4ee73d03009b58f8"), "sno" : 10, "sname" : "pratap", "address" : "hyderabad" }
{ "_id" : ObjectId("6318355e4ee73d03009b58fd"), "sno" : 10, "sname" : "pratap", "address" : "hyderabad" }
>
>
>
> db.students.update({sno:10},{sno:11,sname:"RK",address:"hyderabad"},{upsert:true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>

>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sal" : 2000 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sal" : 2000 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "sal" : 2000 }
{ "_id" : ObjectId("631835354ee73d03009b58ec"), "sno" : 11, "sname" : "RK", "address" : "hyderabad" }
{ "_id" : ObjectId("6318355d4ee73d03009b58f8"), "sno" : 10, "sname" : "pratap", "address" : "hyderabad" }
{ "_id" : ObjectId("6318355e4ee73d03009b58fd"), "sno" : 10, "sname" : "pratap", "address" : "hyderabad" }
>
>
> db.students.update({sno:10},{sno:11,sname:"RK",address:"hyderabad"},{upsert:true},{multi:true})
2022-09-07T11:39:48.181+0530 E QUERY [js] uncaught exception: Error: Fourth argument must be empty when specifying upsert and multi with an object. :
DBCollection.prototype._parseUpdate@src/mongo/shell/collection.js:456:19
DBCollection.prototype.update@src/mongo/shell/collection.js:492:18
@(shell):1:1
>
>
> db.students.update({sno:10},{sno:11,sname:"RK",address:"hyderabad"},{upsert:true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sal" : 2000 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sal" : 2000 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "sal" : 2000 }
{ "_id" : ObjectId("631835354ee73d03009b58ec"), "sno" : 11, "sname" : "RK", "address" : "hyderabad" }
{ "_id" : ObjectId("6318355d4ee73d03009b58f8"), "sno" : 11, "sname" : "RK", "address" : "hyderabad" }
{ "_id" : ObjectId("6318355e4ee73d03009b58fd"), "sno" : 10, "sname" : "pratap", "address" : "hyderabad" }
>
>
>
> db.students.update({sno:10},{sno:11},{upsert:true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sal" : 2000 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sal" : 2000 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "sal" : 2000 }
{ "_id" : ObjectId("631835354ee73d03009b58ec"), "sno" : 11, "sname" : "RK", "address" : "hyderabad" }
{ "_id" : ObjectId("6318355d4ee73d03009b58f8"), "sno" : 11, "sname" : "RK", "address" : "hyderabad" }
{ "_id" : ObjectId("6318355e4ee73d03009b58fd"), "sno" : 11 }


========================================================================================================================================
Remove the data from existing documents
==================================================================================================================================
> db.students.find()
{ "_id" : ObjectId("63182e332040a5ace4df369f"), "sal" : 2000 }
{ "_id" : ObjectId("63182e352040a5ace4df36a0"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e562040a5ace4df36a3"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e632040a5ace4df36a4"), "sno" : 2, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e752040a5ace4df36a5"), "sno" : 20, "sal" : 2000 }
{ "_id" : ObjectId("63182e822040a5ace4df36a6"), "sno" : 3, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e882040a5ace4df36a7"), "sno" : 4, "sname" : "mahendra", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182e982040a5ace4df36a8"), "sno" : 5, "sname" : "Praveen", "address" : "hyd", "sal" : 2000 }
{ "_id" : ObjectId("63182f252040a5ace4df36a9"), "sno" : 6, "address" : "Manikonda", "sname" : "Ravi", "sal" : 2000 }
{ "_id" : ObjectId("631835354ee73d03009b58ec"), "sno" : 11, "sname" : "RK", "address" : "hyderabad" }
{ "_id" : ObjectId("6318355d4ee73d03009b58f8"), "sno" : 11, "sname" : "RK", "address" : "hyderabad" }
{ "_id" : ObjectId("6318355e4ee73d03009b58fd"), "sno" : 11 }
>
>
>
> db.students.remove({sno:1},1)
WriteResult({ "nRemoved" : 1 })
>
>
> db.students.find({sno:1})
{ "_id" : ObjectId("63182e362040a5ace4df36a1"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
{ "_id" : ObjectId("63182e442040a5ace4df36a2"), "sno" : 1, "sname" : "Balakrishna", "address" : "hyd", "sal" : 0 }
>
>
> db.students.remove({sno:1})
WriteResult({ "nRemoved" : 2 })
>
> db.students.remove({sno:11})
WriteResult({ "nRemoved" : 3 })
>
> db.students.remove({})
WriteResult({ "nRemoved" : 8 })
>
>
>
> db.students.find()


=========================================================================================================================
Renaming existing collection
========================================================================================================================
> show collections
colors
products
students
>
>
>
> db.products.renameCollection("Items")
{ "ok" : 1 }
>
>
> show collections
Items
colors
students


===========================================================================================================================
Removing the existing collections
=========================================================================================================================

> show collections
Items
colors
students
>
>
> db.students.drop()
true
>
>
> db.colors.drop()
true
>
> show collections
Items




================================================================================================================================
CRUD-3
=================================================================================================================================
ordered bulk operations
=============================
When Executing an ordered list of operations , MongoDB groups the operation type and contiguity.
With an ordered operations list, MongoDB executes the write operations in serial manner.
Each group of operations can have at most 1000 operations.
If a group exceeds this limit, MongoDB will divide the group into smaller groups of 1000 or less.
var bulk = db.items.initializeOrderedBulkOp()
bulk.insert( { _id: 1,item: “Mobile” , available: true, soldQty:700} );
bulk.insert( { _id: 2,item: “Mp3” , available: false, soldQty:900} );
bulk.insert( { _id: 3,item: “Digicam” , available: true, soldQty:600} );
bulk.execute();


unordered bulk operations
=================================================================
With an unordered operations list, MongoDB can execute in parallel in a nondeterministic fashion.
With an unordered bulk operation, the operations in the list may be reordered to increase performance.
var bulk = db.items.initializeUnorderedBulkOp();
bulk.insert( { _id: 1,item: “Mobile” , available: true, soldQty:700} );
bulk.insert( { _id: 2,item: “Mp3” , available: false, soldQty:900} );
bulk.insert( { _id: 3,item: “Digicam” , available: true, soldQty:600} );
bulk.execute();


===================================================== Misc Commands ============================================================

load("myScript.js")

Create


db.coll.insertOne({name: "Max"})
db.coll.insert([{name: "Max"}, {name:"Alex"}]) // ordered bulk insert
db.coll.insert([{name: "Max"}, {name:"Alex"}], {ordered: false}) // unordered bulk insert
db.coll.insert({date: ISODate()})
db.coll.insert({name: "Max"}, {"writeConcern": {"w": "majority", "wtimeout": 5000}})

Read

db.coll.findOne() // returns a single document
db.coll.find() // returns a cursor - show 20 results - "it" to display more
db.coll.find().pretty()
db.coll.find({name: "Max", age: 32}) // implicit logical "AND".
db.coll.find({date: ISODate("2020-09-25T13:57:17.180Z")})
db.coll.find({name: "Max", age: 32}).explain("executionStats") // or "queryPlanner" or "allPlansExecution"
db.coll.distinct("name")

// Count
db.coll.count({age: 32}) // estimation based on collection metadata
db.coll.estimatedDocumentCount() // estimation based on collection metadata
db.coll.countDocuments({age: 32}) // alias for an aggregation pipeline - accurate count

// Comparison
db.coll.find({"year": {$gt: 1970}})
db.coll.find({"year": {$gte: 1970}})
db.coll.find({"year": {$lt: 1970}})
db.coll.find({"year": {$lte: 1970}})
db.coll.find({"year": {$ne: 1970}})
db.coll.find({"year": {$in: [1958, 1959]}})
db.coll.find({"year": {$nin: [1958, 1959]}})

// Logical
db.coll.find({name:{$not: {$eq: "Max"}}})
db.coll.find({$or: [{"year" : 1958}, {"year" : 1959}]})
db.coll.find({$nor: [{price: 1.99}, {sale: true}]})
db.coll.find({
$and: [
{$or: [{qty: {$lt :10}}, {qty :{$gt: 50}}]},
{$or: [{sale: true}, {price: {$lt: 5 }}]}
]
})

// Element
db.coll.find({name: {$exists: true}})
db.coll.find({"zipCode": {$type: 2 }})
db.coll.find({"zipCode": {$type: "string"}})

// Aggregation Pipeline
db.coll.aggregate([
{$match: {status: "A"}},
{$group: {_id: "$cust_id", total: {$sum: "$amount"}}},
{$sort: {total: -1}}
])

// Text search with a "text" index
db.coll.find({$text: {$search: "cake"}}, {score: {$meta: "textScore"}}).sort({score: {$meta: "textScore"}})

// Regex
db.coll.find({name: /^Max/}) // regex: starts by letter "M"
db.coll.find({name: /^Max$/i}) // regex case insensitive

// Array
db.coll.find({tags: {$all: ["Realm", "Charts"]}})
db.coll.find({field: {$size: 2}}) // impossible to index - prefer storing the size of the array & update it
db.coll.find({results: {$elemMatch: {product: "xyz", score: {$gte: 8}}}})

// Projections
db.coll.find({"x": 1}, {"actors": 1}) // actors + _id
db.coll.find({"x": 1}, {"actors": 1, "_id": 0}) // actors
db.coll.find({"x": 1}, {"actors": 0, "summary": 0}) // all but "actors" and "summary"

// Sort, skip, limit
db.coll.find({}).sort({"year": 1, "rating": -1}).skip(10).limit(3)

// Read Concern
db.coll.find().readConcern("majority")


Update


db.coll.update({"_id": 1}, {"year": 2016}) // WARNING! Replaces the entire document
db.coll.update({"_id": 1}, {$set: {"year": 2016, name: "Max"}})
db.coll.update({"_id": 1}, {$unset: {"year": 1}})
db.coll.update({"_id": 1}, {$rename: {"year": "date"} })
db.coll.update({"_id": 1}, {$inc: {"year": 5}})
db.coll.update({"_id": 1}, {$mul: {price: NumberDecimal("1.25"), qty: 2}})
db.coll.update({"_id": 1}, {$min: {"imdb": 5}})
db.coll.update({"_id": 1}, {$max: {"imdb": 8}})
db.coll.update({"_id": 1}, {$currentDate: {"lastModified": true}})
db.coll.update({"_id": 1}, {$currentDate: {"lastModified": {$type: "timestamp"}}})

// Array
db.coll.update({"_id": 1}, {$push :{"array": 1}})
db.coll.update({"_id": 1}, {$pull :{"array": 1}})
db.coll.update({"_id": 1}, {$addToSet :{"array": 2}})
db.coll.update({"_id": 1}, {$pop: {"array": 1}}) // last element
db.coll.update({"_id": 1}, {$pop: {"array": -1}}) // first element
db.coll.update({"_id": 1}, {$pullAll: {"array" :[3, 4, 5]}})
db.coll.update({"_id": 1}, {$push: {scores: {$each: [90, 92, 85]}}})
db.coll.updateOne({"_id": 1, "grades": 80}, {$set: {"grades.$": 82}})
db.coll.updateMany({}, {$inc: {"grades.$[]": 10}})
db.coll.update({}, {$set: {"grades.$[element]": 100}}, {multi: true, arrayFilters: [{"element": {$gte: 100}}]})

// Update many
db.coll.update({"year": 1999}, {$set: {"decade": "90's"}}, {"multi":true})
db.coll.updateMany({"year": 1999}, {$set: {"decade": "90's"}})

// FindOneAndUpdate
db.coll.findOneAndUpdate({"name": "Max"}, {$inc: {"points": 5}}, {returnNewDocument: true})

// Upsert
db.coll.update({"_id": 1}, {$set: {item: "apple"}, $setOnInsert: {defaultQty: 100}}, {upsert: true})

// Replace
db.coll.replaceOne({"name": "Max"}, {"firstname": "Maxime", "surname": "Beugnet"})

// Save
db.coll.save({"item": "book", "qty": 40})

// Write concern
db.coll.update({}, {$set: {"x": 1}}, {"writeConcern": {"w": "majority", "wtimeout": 5000}})

Delete

db.coll.remove({name: "Max"})
db.coll.remove({name: "Max"}, {justOne: true})
db.coll.remove({}) // WARNING! Deletes all the docs but not the collection itself and its index definitions
db.coll.remove({name: "Max"}, {"writeConcern": {"w": "majority", "wtimeout": 5000}})
db.coll.findOneAndDelete({"name": "Max"})


Databases and Collections


Drop

db.coll.drop() // removes the collection and its index definitions
db.dropDatabase() // double check that you are *NOT* on the PROD cluster... :-)

Create Collection

// Create collection with a $jsonschema
db.createCollection("contacts", {
validator: {$jsonSchema: {
bsonType: "object",
required: ["phone"],
properties: {
phone: {
bsonType: "string",
description: "must be a string and is required"
},
email: {
bsonType: "string",
pattern: "@mongodb\.com$",
description: "must be a string and match the regular expression pattern"
},
status: {
enum: [ "Unknown", "Incomplete" ],
description: "can only be one of the enum values"
}
}
}}
})


Other Collection Functions


db.coll.stats()
db.coll.storageSize()
db.coll.totalIndexSize()
db.coll.totalSize()
db.coll.validate({full: true})
db.coll.renameCollection("new_coll", true) // 2nd parameter to drop the target collection if exists

Indexes
List Indexes

db.coll.getIndexes()
db.coll.getIndexKeys()

Create Indexes


// Index Types
db.coll.createIndex({"name": 1}) // single field index
db.coll.createIndex({"name": 1, "date": 1}) // compound index
db.coll.createIndex({foo: "text", bar: "text"}) // text index
db.coll.createIndex({"$**": "text"}) // wildcard text index
db.coll.createIndex({"userMetadata.$**": 1}) // wildcard index
db.coll.createIndex({"loc": "2d"}) // 2d index
db.coll.createIndex({"loc": "2dsphere"}) // 2dsphere index
db.coll.createIndex({"_id": "hashed"}) // hashed index

// Index Options
db.coll.createIndex({"lastModifiedDate": 1}, {expireAfterSeconds: 3600}) // TTL index
db.coll.createIndex({"name": 1}, {unique: true})
db.coll.createIndex({"name": 1}, {partialFilterExpression: {age: {$gt: 18}}}) // partial index
db.coll.createIndex({"name": 1}, {collation: {locale: 'en', strength: 1}}) // case insensitive index with strength = 1 or 2
db.coll.createIndex({"name": 1 }, {sparse: true})

Drop Indexes

db.coll.dropIndex("name_1")


Hide/Unhide Indexes

db.coll.hideIndex("name_1")
db.coll.unhideIndex("name_1")


######################################## Aggregation Queries ####################################################

https://www.linkedin.com/in/mutyalasrinivas/

Offer Code : atlas_search_2022


https://university.mongodb.com

Download : http://media.mongodb.org/zips.json

db.zips.aggregate({ $group: {_id: '$state', sumOfZips : { $sum: 1 } }})
db.zips.aggregate({ $group: {_id: '$state', sumOfZips : { $sum: 1 } }}, {$sort: {sumOfZips: -1 } })
db.zips.aggregate({ $group: {_id: '$state', sumOfZips : { $sum: 1 } }}, {$sort: {sumOfZips: 1 } })

db.zips.aggregate({ $group: {_id: '$state', population : { $sum: '$pop' } }}, {$sort: {population: 1}})
db.zips.aggregate({ $group: {_id: '$state', population : { $sum: '$pop' } }}, {$sort: {population: -1}})

// Limit the above to states with population >= 10 million


db.zips.aggregate ( 
{$group: {_id: '$state',
population: {$sum: '$pop'}}}, 
{$match: {population: {$gte: 10*1000*1000}}}, 
{$sort: {_id: 1}}
)




db.orders.insertMany([
{ cust_id: 'Ant O. Knee', price: 25 },
{ cust_id: 'Ant O. Knee', price: 70 },
{ cust_id: 'Busby Bee', price: 50 },
{ cust_id: 'Busby Bee', price: 25 },
{ cust_id: 'Busby Bee', price: 50 },
{ cust_id: 'Cam Elot', price: 35 },
{ cust_id: 'Cam Elot', price: 25 },
{ cust_id: 'Don Quis', price: 75 },
{ cust_id: 'Don Quis', price: 55 },
{ cust_id: 'Don Quis', price: 25 }
])


db.orders.aggregate([
{ $group: { _id: "$cust_id", value: { $sum: "$price" } } },
{ $out: "agg_alternative_1" }
])





-
######################################## Range based sharding ####################################################





mkdir d1 d2 c1
mongod --replSet cfg --configsvr --dbpath c1/ --port 36000 --logpath ./cfg --bind_ip_all --fork
mongosh --quiet --port 36000 --eval "rs.initiate()"
mongosh --quiet --port 36000 --eval "rs.status().members"

mongos --configdb 'cfg/localhost.localdomain:36000' --port 10000 --logpath ./mongos --bind_ip_all --fork
mongod --shardsvr --replSet shard1 --dbpath d1/ --port 27010 --logpath ./shard1 --bind_ip_all --fork
mongosh --quiet --port 27010 --eval "rs.initiate()"
mongod --shardsvr --replSet shard2 --dbpath d2/ --port 27020 --logpath ./shard2 --bind_ip_all --fork
mongosh --quiet --port 27020 --eval "rs.initiate()"

ps -ef | grep mongo

mongosh --port 10000 --quiet
sh.status()
sh.addShard('shard1/localhost.localdomain:27010')
sh.status()
sh.addShard('shard2/localhost.localdomain:27020')
sh.status()

// Changing the Chunk size from 64 MB to 1 MB

use config
db.settings.insert( { _id:'chunksize', value: 1 } )

######################################## Range based sharding ####################################################

use shard
sh.enableSharding('shard')
db.shard_col.createIndex( { userid : 1 }, { unique : true })
sh.shardCollection('shard.shard_col', {'userid':1})
for(i=1;i<55000;i++){db.shardcol.insertMany([{userid:i, data: 'useful information'}])}
exit

// To check the counts at each individual Shard1

mongosh "mongodb://localhost.localdomain:27010/shard" --quiet --eval "db.shardcol.countDocuments()"
mongosh "mongodb://localhost.localdomain:27020/shard" --quiet --eval "db.shardcol.countDocuments()"




######################################## Hash sharding ####################################################
use hashdb
sh.enableSharding("hashdb")
db.hashcol.createIndex( { userid : "hashed" })
sh.shardCollection("hashdb.hashcol", { userid: "hashed" })
for(var i = 1; i<= 5000 ; i++){db.hashcol.insert({"userid" : i , "name" : "bulk-inserts", " Iteration: ": i });}
for(i=15000;i<=70000;i++){db.shardcol.insertMany([{userid:i, data: 'useful information'}])}

######################################## Tag-aware sharding ##################################################


sh.addShardTag("shard1", "USERS1")
sh.addShardTag("shard2", "USERS2")

sh.addTagRange("mydb.mycol", { zipcode: 1 }, { zipcode: 100 }, "USERS1") 
sh.addTagRange("mydb.mycol", { zipcode: 101 }, { zipcode: 200 }, "USERS2")

use mydb
sh.enableSharding("mydb")
db.mycol.createIndex( { zipcode : 1 }, { unique : true })
sh.shardCollection("mydb.mycol", { zipcode: 1})

for(var i = 1; i<= 200 ; i++){db.mycol.insert({"zipcode" : i , "name" : "bulk-inserts", " Iteration: ": i });}

db.mycol.find({zipcode: {$gte: 1, $lt: 100 }}).explain('executionStats')
db.mycol.find({zipcode: {$gte: 101 ,$lt: 200 }}).explain('executionStats')






################################################ Building a Sharded Cluster ###########################################################################

mkdir d1 d2 c1
mongod --replSet cfg --configsvr --dbpath c1/ --port 36000 --logpath ./cfg --bind_ip_all --fork
mongosh --quiet --port 36000 --eval "rs.initiate()"
mongosh --quiet --port 36000 --eval "rs.status().members"

mongos --configdb 'cfg/ilocalhost.localdomain:36000' --port 10000 --logpath ./mongos --bind_ip_all --fork
mongod --shardsvr --replSet shard1 --dbpath d1/ --port 27010 --logpath ./shard1 --bind_ip_all --fork
mongosh --quiet --port 27010 --eval "rs.initiate()"
mongod --shardsvr --replSet shard2 --dbpath d2/ --port 27020 --logpath ./shard2 --bind_ip_all --fork
mongosh --quiet --port 27020 --eval "rs.initiate()"

ps -ef | grep mongo

mongosh --port 10000 --quiet
sh.status()
sh.addShard('shard1/ilocalhost.localdomain:27010')
sh.status()
sh.addShard('shard2/ilocalhost.localdomain:27020')
sh.status()


use config
db.settings.insert( { _id:'chunksize', value: 1 } ) // Changing the Chunk size from 64 MB to 1 MB

use admin
sh.enableSharding('shard')
use shard
db.shard_col.createIndex( { userid : 1 }, { unique : true })
use admin
sh.shardCollection('shard.shard_col', {'userid':1})
use shard
for(i=1;i<15000;i++){db.shardcol.insertMany([{userid:i, data: 'useful information'}])}


mongosh "mongodb://ilocalhost.localdomain:27010/shard" --quiet --eval "db.shardcol.countDocuments()"
mongosh "mongodb://ilocalhost.localdomain:27020/shard" --quiet --eval "db.shardcol.countDocuments()"


################################################ Tesing MongoDB backup & restore ###########################################################################

mongodump --port 27030 --out backup/ ( Full ) 
mongodump --port 27030 --oplog --out backup/ ( PIT ) 
mongorestore --oplogReplay --port 36000 /home/ec2-user/backup/ ( restore with PIT ) 
mongorestore "mongodb://ilocalhost.localdomain:36000" --nsInclude='pit.pit_col' /home/ec2-user/backup/

##################### renaming a database & collection while restoring it #####################
mongorestore "mongodb://ilocalhost.localdomain:36000" --nsFrom 'pit.pit_col' --nsTo 'newpit.new_pit_col' /home/ec2-user/backup/

################################################ How opLog Imdempotent ? ###########################################################################

use employees
db.salaries.insertMany([{name:'srinivas', salary: 15000 }])
db.salaries.updateMany({},{$inc: { salary: 100} })
use local
db.oplog.rs.find({ns: 'employees.salaries'})


################### How to create tags to replica members to route your queries#############################################################################

var cnf = rs.conf()
cnf.members[0].tags = {'purpose': 'reporting'}
cnf.members[1].tags = {'purpose': 'alternate_for_reads'}
cnf.members[2].tags = {'purpose': 'production'}

rs.reconfig(cnf)
rs.conf().members

db.newcol.find({name: 'srinivas2'}).readPref('nearest', [{purpose: 'reporting'}])


######################### How to test Hidden, SlaveDelaySeconds and Priority 0 #######################################################################

var cnf = rs.conf()
cnf.members[0].priority = 0
cnf.members[0].secondaryDelaySecs= Long("30")
cnf.members[0].hidden = true
rs.reconfig(cnf)

use newdb
for(i=1;i<101;i++){db.newcol.insertMany([{_id:i,name:'srinivas'+i}])}

At 27010

db.getMongo().setReadPref('secondary')
use newdb
db.newcol.find()


########################## How to do configuration changes in MongoDB ##################################################################################

Enterprise accenture [direct: primary] test> var cnf = rs.conf()
Enterprise accenture [direct: primary] test> cnf.members
Enterprise accenture [direct: primary] test> cnf.members[2].priority = 5
Enterprise accenture [direct: primary] test> cnf.members[1].priority = 3
Enterprise accenture [direct: primary] test> cnf.members
Enterprise accenture [direct: primary] test> rs.reconfig(cnf)
Enterprise accenture [direct: primary] test>

## this is how you can see the change

Enterprise accenture [direct: secondary] test>
Enterprise accenture [direct: secondary] test> db = connect('ilocalhost.localdomain:27030')
Enterprise accenture [direct: primary] test> rs.conf()


################################ Building a ReplicaSet ######################################################################

cd /home/user
mkdir data1 data2 data3

mongod --dbpath data1 --replSet accenture --logpath ./mongod1.log --port 27010 --bind_ip_all --fork
mongod --dbpath data2 --replSet accenture --logpath ./mongod2.log --port 27020 --bind_ip_all --fork
mongod --dbpath data3 --replSet accenture --logpath ./mongod3.log --port 27030 --bind_ip_all --fork

mongosh --port 27010 --quiet
rs.initiate()

use mydb
for(i=1;i<101;i++){db.mycol.insertMany([{_id:i,name:'srinivas'+i}])}

rs.status().members
rs.add('localhost.localdomain:27020')
rs.add('localhost.localdomain:27030')
rs.status().members

### Reading it from Secondary : You'll need to execute below command.

db.getMongo().setReadPref('secondary')
use mydb 
db.mycol.find().limit(2)

### To switch to any other host from mongoshell itself.

db = connect(''localhost.localdomain:27010')
db = connect(''localhost.localdomain:27020')
db = connect(''localhost.localdomain:27030')

###### Test case 1 : Shutdown the primary and see whther existing any of Secondaries becoming Primary

Switch to Primary & perform below.

db = connect('localhost.localdomain:27010')
use admin
db.shutdownServer()

Switch to any of Seoncary & perform below.

db = connect('localhost.localdomain:27020')
rs.status().members

db = connect('localhost.localdomain:27030')
rs.status().members


###### Test case 2 : Shutdown the newly promoted primary and see whther existing Secondary becoming Primary or not

Switch to Primary & perform below.

db = connect('localhost.localdomain:27020')
use admin
db.shutdownServer()

Switch to Seoncary & perform below.

db = connect('localhost.localdomain:27030')
rs.status().members

###### Test case 3 : Bring back the services one by one and see whther existing Secondary(/es) are becoming Primary or not

mongod --dbpath data1 --replSet accenture --logpath ./mongod1.log --port 27010 --bind_ip_all --fork
mongosh --port 27010 --quiet --eval "rs.status().members"

mongod --dbpath data2 --replSet accenture --logpath ./mongod2.log --port 27020 --bind_ip_all --fork
mongosh --port 27020 --quiet --eval "rs.status().members"

###### Test case 4 : Once all the nodes are up & running - run rs.stepDown() at Primary node - press multiple Enters (button) - share the observation.

## Assuming 27030 this is your PRIMARY
mongosh --port 27030 --quiet 
rs.stepDown()

###### Test case 5 : Start an Additional node as below and add it at Primary as below method & share your observation.

cd /home/user
mkdir arb
mongod --dbpath arb --replSet accenture --logpath ./arb.log --port 27040 --bind_ip_all --fork

################################ Assuming 27010 this is your PRIMARY ################################

mongosh --port 27010 --quiet

db.adminCommand({
"setDefaultRWConcern" : 1,
"defaultWriteConcern" : {
"w" : 2
}
})


rs.addArb('localhost.localdomain:27040')
rs.status().members







========================================================================


######################################## Aggregation Queries ####################################################

https://www.linkedin.com/in/mutyalasrinivas/

Offer Code : atlas_search_2022


https://university.mongodb.com

Download : http://media.mongodb.org/zips.json

db.zips.aggregate({ $group: {_id: '$state', sumOfZips : { $sum: 1 } }})
db.zips.aggregate({ $group: {_id: '$state', sumOfZips : { $sum: 1 } }}, {$sort: {sumOfZips: -1 } })
db.zips.aggregate({ $group: {_id: '$state', sumOfZips : { $sum: 1 } }}, {$sort: {sumOfZips: 1 } })

db.zips.aggregate({ $group: {_id: '$state', population : { $sum: '$pop' } }}, {$sort: {population: 1}})
db.zips.aggregate({ $group: {_id: '$state', population : { $sum: '$pop' } }}, {$sort: {population: -1}})

// Limit the above to states with population >= 10 million


db.zips.aggregate ( 
{$group: {_id: '$state',
population: {$sum: '$pop'}}}, 
{$match: {population: {$gte: 10*1000*1000}}}, 
{$sort: {_id: 1}}
)




db.orders.insertMany([
{ cust_id: 'Ant O. Knee', price: 25 },
{ cust_id: 'Ant O. Knee', price: 70 },
{ cust_id: 'Busby Bee', price: 50 },
{ cust_id: 'Busby Bee', price: 25 },
{ cust_id: 'Busby Bee', price: 50 },
{ cust_id: 'Cam Elot', price: 35 },
{ cust_id: 'Cam Elot', price: 25 },
{ cust_id: 'Don Quis', price: 75 },
{ cust_id: 'Don Quis', price: 55 },
{ cust_id: 'Don Quis', price: 25 }
])


db.orders.aggregate([
{ $group: { _id: "$cust_id", value: { $sum: "$price" } } },
{ $out: "agg_alternative_1" }
])





-
######################################## Range based sharding ####################################################





mkdir d1 d2 c1
mongod --replSet cfg --configsvr --dbpath c1/ --port 36000 --logpath ./cfg --bind_ip_all --fork
mongosh --quiet --port 36000 --eval "rs.initiate()"
mongosh --quiet --port 36000 --eval "rs.status().members"

mongos --configdb 'cfg/localhost.localdomain:36000' --port 10000 --logpath ./mongos --bind_ip_all --fork
mongod --shardsvr --replSet shard1 --dbpath d1/ --port 27010 --logpath ./shard1 --bind_ip_all --fork
mongosh --quiet --port 27010 --eval "rs.initiate()"
mongod --shardsvr --replSet shard2 --dbpath d2/ --port 27020 --logpath ./shard2 --bind_ip_all --fork
mongosh --quiet --port 27020 --eval "rs.initiate()"

ps -ef | grep mongo

mongosh --port 10000 --quiet
sh.status()
sh.addShard('shard1/localhost.localdomain:27010')
sh.status()
sh.addShard('shard2/localhost.localdomain:27020')
sh.status()

// Changing the Chunk size from 64 MB to 1 MB

use config
db.settings.insert( { _id:'chunksize', value: 1 } )

######################################## Range based sharding ####################################################

use shard
sh.enableSharding('shard')
db.shard_col.createIndex( { userid : 1 }, { unique : true })
sh.shardCollection('shard.shard_col', {'userid':1})
for(i=1;i<55000;i++){db.shardcol.insertMany([{userid:i, data: 'useful information'}])}
exit

// To check the counts at each individual Shard1

mongosh "mongodb://localhost.localdomain:27010/shard" --quiet --eval "db.shardcol.countDocuments()"
mongosh "mongodb://localhost.localdomain:27020/shard" --quiet --eval "db.shardcol.countDocuments()"




######################################## Hash sharding ####################################################
use hashdb
sh.enableSharding("hashdb")
db.hashcol.createIndex( { userid : "hashed" })
sh.shardCollection("hashdb.hashcol", { userid: "hashed" })
for(var i = 1; i<= 5000 ; i++){db.hashcol.insert({"userid" : i , "name" : "bulk-inserts", " Iteration: ": i });}
for(i=15000;i<=70000;i++){db.shardcol.insertMany([{userid:i, data: 'useful information'}])}

######################################## Tag-aware sharding ##################################################


sh.addShardTag("shard1", "USERS1")
sh.addShardTag("shard2", "USERS2")

sh.addTagRange("mydb.mycol", { zipcode: 1 }, { zipcode: 100 }, "USERS1") 
sh.addTagRange("mydb.mycol", { zipcode: 101 }, { zipcode: 200 }, "USERS2")

use mydb
sh.enableSharding("mydb")
db.mycol.createIndex( { zipcode : 1 }, { unique : true })
sh.shardCollection("mydb.mycol", { zipcode: 1})

for(var i = 1; i<= 200 ; i++){db.mycol.insert({"zipcode" : i , "name" : "bulk-inserts", " Iteration: ": i });}

db.mycol.find({zipcode: {$gte: 1, $lt: 100 }}).explain('executionStats')
db.mycol.find({zipcode: {$gte: 101 ,$lt: 200 }}).explain('executionStats')






################################################ Building a Sharded Cluster ###########################################################################

mkdir d1 d2 c1
mongod --replSet cfg --configsvr --dbpath c1/ --port 36000 --logpath ./cfg --bind_ip_all --fork
mongosh --quiet --port 36000 --eval "rs.initiate()"
mongosh --quiet --port 36000 --eval "rs.status().members"

mongos --configdb 'cfg/ilocalhost.localdomain:36000' --port 10000 --logpath ./mongos --bind_ip_all --fork
mongod --shardsvr --replSet shard1 --dbpath d1/ --port 27010 --logpath ./shard1 --bind_ip_all --fork
mongosh --quiet --port 27010 --eval "rs.initiate()"
mongod --shardsvr --replSet shard2 --dbpath d2/ --port 27020 --logpath ./shard2 --bind_ip_all --fork
mongosh --quiet --port 27020 --eval "rs.initiate()"

ps -ef | grep mongo

mongosh --port 10000 --quiet
sh.status()
sh.addShard('shard1/ilocalhost.localdomain:27010')
sh.status()
sh.addShard('shard2/ilocalhost.localdomain:27020')
sh.status()


use config
db.settings.insert( { _id:'chunksize', value: 1 } ) // Changing the Chunk size from 64 MB to 1 MB

use admin
sh.enableSharding('shard')
use shard
db.shard_col.createIndex( { userid : 1 }, { unique : true })
use admin
sh.shardCollection('shard.shard_col', {'userid':1})
use shard
for(i=1;i<15000;i++){db.shardcol.insertMany([{userid:i, data: 'useful information'}])}


mongosh "mongodb://ilocalhost.localdomain:27010/shard" --quiet --eval "db.shardcol.countDocuments()"
mongosh "mongodb://ilocalhost.localdomain:27020/shard" --quiet --eval "db.shardcol.countDocuments()"


################################################ Tesing MongoDB backup & restore ###########################################################################

mongodump --port 27030 --out backup/ ( Full ) 
mongodump --port 27030 --oplog --out backup/ ( PIT ) 
mongorestore --oplogReplay --port 36000 /home/ec2-user/backup/ ( restore with PIT ) 
mongorestore "mongodb://ilocalhost.localdomain:36000" --nsInclude='pit.pit_col' /home/ec2-user/backup/

##################### renaming a database & collection while restoring it #####################
mongorestore "mongodb://ilocalhost.localdomain:36000" --nsFrom 'pit.pit_col' --nsTo 'newpit.new_pit_col' /home/ec2-user/backup/

################################################ How opLog Imdempotent ? ###########################################################################

use employees
db.salaries.insertMany([{name:'srinivas', salary: 15000 }])
db.salaries.updateMany({},{$inc: { salary: 100} })
use local
db.oplog.rs.find({ns: 'employees.salaries'})


################### How to create tags to replica members to route your queries#############################################################################

var cnf = rs.conf()
cnf.members[0].tags = {'purpose': 'reporting'}
cnf.members[1].tags = {'purpose': 'alternate_for_reads'}
cnf.members[2].tags = {'purpose': 'production'}

rs.reconfig(cnf)
rs.conf().members

db.newcol.find({name: 'srinivas2'}).readPref('nearest', [{purpose: 'reporting'}])


######################### How to test Hidden, SlaveDelaySeconds and Priority 0 #######################################################################

var cnf = rs.conf()
cnf.members[0].priority = 0
cnf.members[0].secondaryDelaySecs= Long("30")
cnf.members[0].hidden = true
rs.reconfig(cnf)

use newdb
for(i=1;i<101;i++){db.newcol.insertMany([{_id:i,name:'srinivas'+i}])}

At 27010

db.getMongo().setReadPref('secondary')
use newdb
db.newcol.find()


########################## How to do configuration changes in MongoDB ##################################################################################

Enterprise accenture [direct: primary] test> var cnf = rs.conf()
Enterprise accenture [direct: primary] test> cnf.members
Enterprise accenture [direct: primary] test> cnf.members[2].priority = 5
Enterprise accenture [direct: primary] test> cnf.members[1].priority = 3
Enterprise accenture [direct: primary] test> cnf.members
Enterprise accenture [direct: primary] test> rs.reconfig(cnf)
Enterprise accenture [direct: primary] test>

## this is how you can see the change

Enterprise accenture [direct: secondary] test>
Enterprise accenture [direct: secondary] test> db = connect('ilocalhost.localdomain:27030')
Enterprise accenture [direct: primary] test> rs.conf()


################################ Building a ReplicaSet ######################################################################

cd /home/user
mkdir data1 data2 data3

mongod --dbpath data1 --replSet accenture --logpath ./mongod1.log --port 27010 --bind_ip_all --fork
mongod --dbpath data2 --replSet accenture --logpath ./mongod2.log --port 27020 --bind_ip_all --fork
mongod --dbpath data3 --replSet accenture --logpath ./mongod3.log --port 27030 --bind_ip_all --fork

mongosh --port 27010 --quiet
rs.initiate()

use mydb
for(i=1;i<101;i++){db.mycol.insertMany([{_id:i,name:'srinivas'+i}])}

rs.status().members
rs.add('localhost.localdomain:27020')
rs.add('localhost.localdomain:27030')
rs.status().members

### Reading it from Secondary : You'll need to execute below command.

db.getMongo().setReadPref('secondary')
use mydb 
db.mycol.find().limit(2)

### To switch to any other host from mongoshell itself.

db = connect(''localhost.localdomain:27010')
db = connect(''localhost.localdomain:27020')
db = connect(''localhost.localdomain:27030')

###### Test case 1 : Shutdown the primary and see whther existing any of Secondaries becoming Primary

Switch to Primary & perform below.

db = connect('localhost.localdomain:27010')
use admin
db.shutdownServer()

Switch to any of Seoncary & perform below.

db = connect('localhost.localdomain:27020')
rs.status().members

db = connect('localhost.localdomain:27030')
rs.status().members


###### Test case 2 : Shutdown the newly promoted primary and see whther existing Secondary becoming Primary or not

Switch to Primary & perform below.

db = connect('localhost.localdomain:27020')
use admin
db.shutdownServer()

Switch to Seoncary & perform below.

db = connect('localhost.localdomain:27030')
rs.status().members

###### Test case 3 : Bring back the services one by one and see whther existing Secondary(/es) are becoming Primary or not

mongod --dbpath data1 --replSet accenture --logpath ./mongod1.log --port 27010 --bind_ip_all --fork
mongosh --port 27010 --quiet --eval "rs.status().members"

mongod --dbpath data2 --replSet accenture --logpath ./mongod2.log --port 27020 --bind_ip_all --fork
mongosh --port 27020 --quiet --eval "rs.status().members"

###### Test case 4 : Once all the nodes are up & running - run rs.stepDown() at Primary node - press multiple Enters (button) - share the observation.

## Assuming 27030 this is your PRIMARY
mongosh --port 27030 --quiet 
rs.stepDown()

###### Test case 5 : Start an Additional node as below and add it at Primary as below method & share your observation.

cd /home/user
mkdir arb
mongod --dbpath arb --replSet accenture --logpath ./arb.log --port 27040 --bind_ip_all --fork

################################ Assuming 27010 this is your PRIMARY ################################

mongosh --port 27010 --quiet

db.adminCommand({
"setDefaultRWConcern" : 1,
"defaultWriteConcern" : {
"w" : 2
}
})


rs.addArb('localhost.localdomain:27040')
rs.status().members







------------------------------------------------------------------------------------------------------------------------------





Handy commands


use admin
db.createUser({"user": "root", "pwd": passwordPrompt(), "roles": ["root"]})
db.dropUser("root")
db.auth( "user", passwordPrompt() )

use test
db.getSiblingDB("dbname")
db.currentOp()
db.killOp(123) // opid

db.fsyncLock()
db.fsyncUnlock()

db.getCollectionNames()
db.getCollectionInfos()
db.printCollectionStats()
db.stats()

db.getReplicationInfo()
db.printReplicationInfo()
db.isMaster()
db.hostInfo()
db.printShardingStatus()
db.shutdownServer()
db.serverStatus()

db.setSlaveOk()
db.getSlaveOk()

db.getProfilingLevel()
db.getProfilingStatus()
db.setProfilingLevel(1, 200) // 0 == OFF, 1 == ON with slowms, 2 == ON

db.enableFreeMonitoring()
db.disableFreeMonitoring()
db.getFreeMonitoringStatus()

db.createView("viewName", "sourceColl", [{$project:{department: 1}}])


Change Streams


watchCursor = db.coll.watch( [ { $match : {"operationType" : "insert" } } ] )

while (!watchCursor.isExhausted()){
if (watchCursor.hasNext()){
print(tojson(watchCursor.next()));
}
}

Replica Set


rs.status()
rs.initiate({"_id": "replicaTest",
members: [
{ _id: 0, host: "127.0.0.1:27017" },
{ _id: 1, host: "127.0.0.1:27018" },
{ _id: 2, host: "127.0.0.1:27019", arbiterOnly:true }]
})
rs.add("mongodbd1.example.net:27017")
rs.addArb("mongodbd2.example.net:27017")
rs.remove("mongodbd1.example.net:27017")
rs.conf()
rs.isMaster()
rs.printReplicationInfo()
rs.printSlaveReplicationInfo()
rs.reconfig(<valid_conf>)
rs.slaveOk()
rs.stepDown(20, 5) // (stepDownSecs, secondaryCatchUpPeriodSecs)

Sharded Cluster


sh.status()
sh.addShard("rs1/mongodbd1.example.net:27017")
sh.shardCollection("mydb.coll", {zipcode: 1})

sh.moveChunk("mydb.coll", { zipcode: "53187" }, "shard0019")
sh.splitAt("mydb.coll", {x: 70})
sh.splitFind("mydb.coll", {x: 70})
sh.disableAutoSplit()
sh.enableAutoSplit()

sh.startBalancer()
sh.stopBalancer()
sh.disableBalancing("mydb.coll")
sh.enableBalancing("mydb.coll")
sh.getBalancerState()
sh.setBalancerState(true/false)
sh.isBalancerRunning()

sh.addTagRange("mydb.coll", {state: "NY", zip: MinKey }, { state: "NY", zip: MaxKey }, "NY")
sh.removeTagRange("mydb.coll", {state: "NY", zip: MinKey }, { state: "NY", zip: MaxKey }, "NY")
sh.addShardTag("shard0000", "NYC")
sh.removeShardTag("shard0000", "NYC")

sh.addShardToZone("shard0000", "JFK")
sh.removeShardFromZone("shard0000", "NYC")
sh.removeRangeFromZone("mydb.coll", {a: 1, b: 1}, {a: 10, b: 10})

  • Ask Question