0% found this document useful (0 votes)
4 views3 pages

MongoDB Notes With Operators

This document provides a comprehensive overview of various MongoDB commands and matched operators, including basic find operations, limit/skip/sort functionalities, comparison and logical operators, and update/delete commands. It also covers element and expression operators, dot notation for embedded fields, and examples of counting and retrieving single results. The document serves as a reference for performing CRUD operations in MongoDB.

Uploaded by

amorvignesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

MongoDB Notes With Operators

This document provides a comprehensive overview of various MongoDB commands and matched operators, including basic find operations, limit/skip/sort functionalities, comparison and logical operators, and update/delete commands. It also covers element and expression operators, dot notation for embedded fields, and examples of counting and retrieving single results. The document serves as a reference for performing CRUD operations in MongoDB.

Uploaded by

amorvignesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MongoDB Notes - Commands & Matched Operators

Basic Find (No operator)

db.users.find()
db.users.find({name: "vicky"})
db.users.find({name: "vicky"}, {name: 1, age: 1})
db.users.find({name: "vicky"}, {name: 1, age: 1, _id: 0})
db.users.find({name: "vicky"}, {age: 0})

Limit / Skip / Sort (Command options)

db.users.find().limit(2)
db.users.find().sort({name: 1}).limit(3)
db.users.find().sort({name: -1}).limit(3)
db.users.find().sort({age: -1, name: -1}).limit(3)
db.users.find().sort({age: 1, name: 1}).limit(3)
db.users.find().skip(1).limit(3)

Comparison Operators

db.users.find({name: {$eq: "vicky"}}) // $eq


db.users.find({name: {$ne: "vicky"}}) // $ne
db.users.find({age: {$gt: 24}}) // $gt
db.users.find({age: {$gte: 24}}) // $gte
db.users.find({age: {$lt: 24}}) // $lt
db.users.find({age: {$lte: 24}}) // $lte
db.users.find({name: {$in: ["vicky","amor"]}}) // $in
db.users.find({name: {$nin: ["vicky","amor"]}}) // $nin

Element Operators

db.users.find({skill: {$exists: true}}) // $exists


db.users.find({skill: {$exists: false}}) // $exists

Range (Combined Comparison)

db.users.find({age: {$gte: 20, $lte: 30}}) // $gte + $lte


MongoDB Notes - Commands & Matched Operators

Logical Operators

db.users.find({$and: [{age: {$gt: 24}}, {city: "chennai"}]}) // $and


db.users.find({$or: [{age: {$gt: 24}}, {city: "chennai"}]}) // $or
db.users.find({age: {$not: {$gte: 20}}}) // $not

Expression Operators

db.users.find({$expr: {$gt: ["$salary", "$bonus"]}}) // $expr

Dot Notation (Embedded Fields)

db.users.find({"address.pincode": 609108})

Count / Single Result

db.users.countDocuments({age: {$gt: 20}})


db.users.findOne({age: {$gt: 20}})

Update Operators

db.users.updateOne({_id: ObjectId('...')}, {$set: {name: "amor*vicky"}}) // $set


db.users.updateOne({name: "city"}, {$set: {name: "vicky"}}) // $set
db.users.updateOne({name: "amor*vicky"}, {$inc: {age: 1}}) // $inc
db.users.updateOne({name: "amorvicky"}, {$rename: {ages: "age"}}) // $rename
db.users.updateOne({name: "amorvicky"}, {$push: {skill: "net"}}) // $push
db.users.updateOne({name: "amorvicky"}, {$pull: {skill: ["node", "net"]}})// $pull
db.users.updateOne({name: "amorvicky"}, {$unset: {skill: ""}}) // $unset
db.users.updateMany({amorvicky: {$exists: true}}, {$unset: {amorvicky: ""}}) // $unset,
$exists

Replace Operator

db.users.replaceOne(
{name: "amorvicky"},
MongoDB Notes - Commands & Matched Operators

{name: "amorvicky", age: 25, address: "koothiyam pettai", contectno: 7010901510,


skill: ["money","game","play outdoor"]}
)

Delete Operators

db.users.deleteOne({name: "amorvicky"})
db.users.deleteMany({contectno: {$exists: true}}) // $exists

You might also like