1.
Run CMD as Admin & Navigate to Mongo DB bin folder
C:\Program Files\MongoDB\Server\4.0>cd bin
2. Run Mongo Shell
C:\Program Files\MongoDB\Server\4.0\bin>mongo
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a9e41a3b-3656-4b7e-940b-5278811050e1") }
MongoDB server version: 4.0.5
Server has startup warnings:
3. Show all DBs
> show dbs
admin 0.000GB
chanchaldb 0.000GB
config 0.000GB
local 0.000GB
4. Go to Any DB
> use chanchaldb
switched to db chanchaldb
5. Create Collection
db.createCollection("collection name")
6. Show all the collections in a DB
>show collections
chanchalcollections
tempcollections
7. Find all the documents in a collection
> db.chanchalcollection.find()
{ "_id" : ObjectId("5c3d7eb9248dc5921bd2a8ae"), "name" : "chanchal", "age" : 27 }
8. Insert document in a collection
> db.chanchalcollection.insert({name:"Shivani",age:30})
WriteResult({ "nInserted" : 1 })
> db.chanchalcollection.find()
{ "_id" : ObjectId("5c3d7eb9248dc5921bd2a8ae"), "name" : "chanchal", "age" : 27 }
{ "_id" : ObjectId("5c3eb24f5c36dbd720df1749"), "name" : "Shivani", "age" : 30 }
9. Drop a collection
> db.tempcollection.drop()
True
10. Drop a Database
>db.dropDatabase()
11. Multi document insert
>> var employees =
... [
... {
... "empId" : 1001,
... "empName" : "Chanchal",
... "age": 28,
... "Skills":[{name:"IBM 8.5.7", experience:3},{name:"java",experience:3}]
... },
... {
... "empId" : 1002,
... "empName" : "Shivani",
... "age": 28,
... "Skills":[{name:"IBM 8.5.7/8.6", experience:5},{name:"java",experience:2}]
... }
... ];
> db.employees.insert(employees);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.employees.find()
{ "_id" : ObjectId("5c3eb40d5c36dbd720df174a"), "empId" : 1001, "empName" : "Chanchal",
"age" : 28, "Skills" : [ { "name" : "IBM 8.5.7", "experience" : 3 }, { "name" : "java", "experience" : 3
}]}
{ "_id" : ObjectId("5c3eb40d5c36dbd720df174b"), "empId" : 1002, "empName" : "Shivani", "age"
: 28, "Skills" : [ { "name" : "IBM 8.5.7/8.6", "experience" : 5 }, { "name" : "java", "experience" : 2 }
]}
> show collections
chanchalcollection
employees
tempcollection2
12. Print a documents of a collection in JSON format>
> db.employees.find().forEach(printjson)
"_id" : ObjectId("5c3eb40d5c36dbd720df174a"),
"empId" : 1001,
"empName" : "Chanchal",
"age" : 28,
"Skills" : [
"name" : "IBM 8.5.7",
"experience" : 3
},
"name" : "java",
"experience" : 3
}
]
"_id" : ObjectId("5c3eb40d5c36dbd720df174b"),
"empId" : 1002,
"empName" : "Shivani",
"age" : 28,
"Skills" : [
"name" : "IBM 8.5.7/8.6",
"experience" : 5
},
"name" : "java",
"experience" : 2
13. Print a documents of a collection in JSON format>
> db.employees.find().pretty()
14. Find a specific document in a collection
> db.employees.find({empName:"Shivani"}).pretty()
For example: I would like to fetch the details of employee having age > 27/ age > 27 then the
query should be:
> db.employees.find({age:{$gt:27}}).pretty()
> db.employees.find({age:{$lt:27}}).pretty()
Not equals:
> db.employees.find({age:{$ne:27}}).pretty()
Greater/lesser than equals:
> db.employees.find({age:{$gte:27}}).pretty()
> db.employees.find({age:{$lte:27}}).pretty()
15. Update fields of a document in a collection for a specific criteria
> db.chanchalcollection.update({name:"chanchal"},{$set:{name:"C#@NC#@!"}})
updates a single row only
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.chanchalcollection.find()
{ "_id" : ObjectId("5c3d7eb9248dc5921bd2a8ae"), "name" : "C#@NC#@!", "age" : 27 }
To update multi document for the filtered criteria:
>db.chanchalcollection.update({name:"chanchal"},{$set:{name:"C#@NC#@!"}},{multi:tru
e})
16. Update a complete document in a collection
>db.chanchalcollection.save({"_id" : ObjectId("5c3eb24f5c36dbd720df1749"), "name":
"Shivani Goyal", "age": 29})
17. Remove documents from a collection
> db.chanchalcollection.remove({name:"temp"})-- remove all matching creiteria
> db.chanchalcollection.remove({name:"temp"},1) -- justOne parameter true
> db.chanchalcollection.remove({}) -- justOne parameter true -- Remove all documents
from a collection
18. Fetch specific columns from documents in a collection:
With ID column-->
> db.chanchalcollection.find({},{name:1})
{ "_id" : ObjectId("5c3d7eb9248dc5921bd2a8ae"), "name" : "C#@NC#@!" }
{ "_id" : ObjectId("5c3eb24f5c36dbd720df1749"), "name" : "Shivani Goyal" }
Without ID column-->
> db.chanchalcollection.find({},{_id:0,name:1})
{ "name" : "C#@NC#@!" }
{ "name" : "Shivani Goyal" }
19. Limit number of records to be fetched
> db.chanchalcollection.find().limit(1)
{ "_id" : ObjectId("5c3d7eb9248dc5921bd2a8ae"), "name" : "C#@NC#@!", "age" : 27 }
20. Skip the number of rows to be fetched
> db.studentdata.find({student_id : {$gt:2002}}).limit(1).skip(1).pretty()
21. Sorting of the results of collections
> db.chanchalcollection.find({},{name:1}).sort({name:1}) – Ascending Sorting
{ "_id" : ObjectId("5c3d7eb9248dc5921bd2a8ae"), "name" : "C#@NC#@!" }
{ "_id" : ObjectId("5c3eb24f5c36dbd720df1749"), "name" : "Shivani Goyal" }
> db.chanchalcollection.find({},{name:1}).sort({name:-1}) – Decending Sorting
{ "_id" : ObjectId("5c3eb24f5c36dbd720df1749"), "name" : "Shivani Goyal" }
{ "_id" : ObjectId("5c3d7eb9248dc5921bd2a8ae"), "name" : "C#@NC#@!" }