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
I'm not sure why but the thing with text search by index doesn't work, I even tried to use the exact same commands as in this sheet, but on db.posts.createIndex({ title: 'text' })
i get just title_text
and then
db.friends.find({
$text: {
$search: "\"V\""
}
})
gives me an empty line. I'm using the mongosh 1.6.2.0 version
Thank you brother, amazing brief Sheet to understand the basics of MongoDB. Every work you produce is always amazing and professional in terms of its content. Thanks for everything.
I would add a second example to 'find' to specify which fields to retrieve. For example: db.posts.find({ category: 'News' }, { title: true, body: true })
This is equivalent in SQL to: SELECT title, body FROM posts WHERE category = 'News'
$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/ } } ] });
Thank You!