MongoDB Notes - Commands & Matched Operators
Basic Find (No Operator)
db.users.find({name:"vicky"})
ex: show all doucmant
db.users.find({name:"vicky"},{name:1,age:1})
ex: show only name and age
db.users.find({name:"vicky"},{name:1,age:1,_id:0})
ex: show name ,age...etc you not see id
db.users.find({name:"vicky"},{age:0})
ex: you not see age only Otherwise you will see name , habbise ,adderss...etc
Limit / Skip / Sort
db.users.find().limit(2)
ex: show the frist 2 doucmant
db.users.find().sort({name:1}).limit(3)
ex: name front
db.users.find().sort({name:-1}).limit(3)
ex: name revers
db.users.find().sort({age:-1,name:-1}).limit(3)
ex: age high to low, name z to a
db.users.find().sort({age:1,name:1}).limit(3)
ex: age low to high, name a to z
db.users.find().skip(1).limit(3)
ex: skip the frist doucmant
Comparison Operators
MongoDB Notes - Commands & Matched Operators
db.users.find({name:{$eq:"vicky"}})
ex: match the Vicky give the data
db.users.find({name:{$ne:"vicky"}})
ex: no show Vicky otherwise you will see all doucmant
db.users.find({age:{$gt:24}})
ex: show the data is age 24 above
db.users.find({age:{$gte:24}})
ex: show the even 24
db.users.find({age:{$lt:24}})
ex: show the age is blow 24
db.users.find({age:{$lte:24}})
ex: show the age 24 to continue
db.users.find({name:{$in:["vicky","amor"]}})
ex: show the Vicky and amor
db.users.find({name:{$nin:["vicky","amor"]}})
ex: no show the Vicky and amor Details
Element Operators
db.users.find({skill:{$exists:true}})
ex: show the skill otherwise no show
db.users.find({skill:{$exists:false}})
ex: no show the skill otherwise you will see
Range Combine (Comparison)
db.users.find({age:{$gte:20,$lte:30}})
ex: you will see age grater then 20 equal age to lessthen 30 equal
MongoDB Notes - Commands & Matched Operators
Logical Operators
db.users.find({$and:[{age:{$gt:24}},{ city:"chennai"}]})
ex: two condition is true you will get the value
db.users.find({$or:[{age:{$gt:24}},{ city:"chennai"}]})
ex: any one condition is true you will get the value
db.users.find({age:{$not:{$gte:20}}})
ex: not greater than or equal to 20
Expression Operator
db.users.find({$expr:{$gt:["$salary","$bonus"]}})
ex: expression is to comper the two field like salary and Bonus
Dot Notation / Embedded Fields
db.users.find({"address.pincode":609108})
ex: query inside address document
Count and FindOne
db.users.countDocuments({age:{$gt:20}})
ex: give the number the documents in number
db.users.findOne({age:{$gt:20}})
ex: give the first orderwise
Update Operators
db.users.updateOne({ _id:
MongoDB Notes - Commands & Matched Operators
ObjectId('685113cc711bcdfbfd50eb68')},{$set:{name:"amor*vicky"}})
ex: update using object id
db.users.updateOne({name:"city"},{$set:{name:"vicky"}})
ex: replace city name to vicky
db.users.updateOne({name:"amor*vicky"},{$inc:{age:1}})
ex: increment the age
db.users.updateOne({name:"amorvicky"},{$rename:{ages:"age"}})
ex: rename ages field to age
db.users.updateOne({name:"amorvicky"},{$push:{skill:"net"}})
ex: add skill into array
db.users.updateOne({name:"amorvicky"},{$pull:{skill:["node","net"]}})
ex: remove node and net skill from array
db.users.updateOne({name:"amorvicky"},{$unset:{skill:""}})
ex: delete the skill field
db.users.updateMany({amorvicky:{$exists:true}},{$unset:{amorvicky:""}})
ex: remove amorvicky field from all documents where exists
Replace Document
db.users.replaceOne({name:"amorvicky"},
{name:"amorvicky", age:25 , address: "koothiyam pettai" , contectno:7010901510 , skill:
["money","game","play outdoor"]})
ex: full replace the document
Delete Operators
db.users.deleteOne({name:"amorvicky"})
ex: delete only one document with name
db.users.deleteMany({contectno:{$exists: true }})
MongoDB Notes - Commands & Matched Operators
ex: delete all document with contectno field