MongoDB 8 cheatsheet
MongoDB 8: Query and update operators
8 common MongoDB 8 tasks.
Match any of several values
Find documents whose field equals one of the listed values.
db.users.find({ role: { $in: ["admin", "editor"] } })
Combine conditions with OR
Find documents that match either condition.
db.users.find({ $or: [{ age: { $lt: 18 } }, { age: { $gt: 65 } }] })
Check that a field exists
Find documents that have an email field.
db.users.find({ email: { $exists: true } })
Match inside an array
Find orders with a line item that satisfies several conditions together.
db.orders.find({ items: { $elemMatch: { sku: "A1", qty: { $gte: 2 } } } })
Add to an array
Append a value to an array field.
db.users.updateOne({ name: "Ada" }, { $push: { tags: "admin" } })
Remove from an array
Remove matching values from an array field.
db.users.updateOne({ name: "Ada" }, { $pull: { tags: "admin" } })
Remove a field
Delete a field from a document.
db.users.updateOne({ name: "Ada" }, { $unset: { legacyId: "" } })