You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ne (Not equals) - Find documents where 'category' is NOT 'News': db.posts.find({ category: { $ne: "News" } });
$in (Matches Any Of) - Find documents where 'category' is either 'News' OR 'Technology': db.posts.find({ category: { $in: ["News", "Technology"] } });
$nin (Not In) - Find documents where 'category' is neither 'News' nor 'Technology': db.posts.find({ category: { $nin: ["News", "Technology"] } });
2) Logical operator examples:
$and (Matches All) - Find documents where 'category' is "News" AND 'user.name' is "John Doe": db.posts.find({ $and: [ { category: "News" }, { "user.name": "John Doe" } ] });
$nor (Matches None) - Find documents where 'category' is neither "News" NOR "Technology": db.posts.find({ $nor: [ { category: "News" }, { category: "Technology" } ] });
$or (Matches Any) - Find documents where 'category' is "News" OR "Technology": db.posts.find({ $or: [ { category: "News" }, { category: "Technology" } ] });
$not (With $lte & $regex) - Find documents where 'views' is NOT less than or equal to 100 (i.e., greater than 100), and the 'title' does not start with "Post": db.posts.find({ $and: [ { views: { $not: { $lte: 100 } } }, { title: { $not: /^Post/ } } ] });
Adding update/delete examples using updateOne, updateMany, deleteOne, deleteMany with $set, and $unset.
1) UPDATE
Update One:
db.posts.updateOne( { title: "Post One" }, { $set: { body: "Updated body for Post One" }, $unset: { category: "" } } );
Update Many:
db.posts.updateMany( { category: "News" }, { $set: { category: "Updated News" } } );
2) DELETE
Delete One:
db.posts.deleteOne( { title: "Post Four" } );
Delete Many
db.posts.deleteMany( { category: "Entertainment" } );