Query and Update Operators
MongoDB operators let you express conditions and updates.
They usually start with $.
Query operators
Greater than:
db.products.find({ price: { $gt: 100 } })
In list:
db.products.find({ category: { $in: ['books', 'electronics'] } })
Or condition:
db.users.find({
$or: [{ role: 'admin' }, { verified: true }],
})
Update operators
Set a field:
db.users.updateOne(
{ email: 'asha@example.com' },
{ $set: { active: true } },
)
Increment:
db.posts.updateOne(
{ _id: postId },
{ $inc: { views: 1 } },
)
Push into array:
db.posts.updateOne(
{ _id: postId },
{ $push: { tags: 'mongodb' } },
)
Common operators
| Operator | Use |
|---|---|
$set |
set field value |
$inc |
increment number |
$push |
add to array |
$in |
match any value in list |
$gt / $lt |
greater than / less than |
$or |
match any condition |
Common mistake
Be careful with update without operators.
Depending on API usage, you may replace more of the document than expected.
Use $set when updating specific fields.
Interview answer
MongoDB query operators define matching conditions, such as $gt, $in, and $or. Update operators modify documents, such as $set, $inc, and $push. Operators make queries and updates expressive without manually reading and rewriting whole documents.