我知道我们可以在选择器中使用find来获取集合中的特定元素,如下所示:
usernames.find(selector)要指定查找具有特定属性(如“Bob”)的所有用户名作为我将执行的名称:
usernames.find({name: "Bob"})这只会显示名为Bob的文档。 但是让我们说,我的收藏中还有其他人喜欢Alice和Kat,我想找到收藏中不是Bob的所有人。
我将如何指定一个选择器来查找名称不是Bob的用户名集合中的所有内容?
我试过了:
usernames.find({name: !"Bob"})我被困在语法上。 谢谢!
I understand we can use find with the selector to get specific elements in a collection like so:
usernames.find(selector)To specify finding all usernames with a specific property like "Bob" as the name I would do:
usernames.find({name: "Bob"})Which would only show me documents that have the name Bob. But let's say there are other people in my collection like Alice and Kat and I want to find everyone in the collection whose name is not Bob.
How would I specify a selector to find everything in the usernames collection whose name is not Bob?
I've tried:
usernames.find({name: !"Bob"})I'm stuck on the syntax. Thank you!
最满意答案
使用$ ne运算符
usernames.find( { name: { $ne: "Bob" } })
$ ne选择字段值不等于指定值的文档。 这包括不包含该字段的文档。
https://docs.mongodb.com/manual/reference/operator/query/ne/
Use $ne operator
usernames.find( { name: { $ne: "Bob" } })
$ne selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.
https://docs.mongodb.com/manual/reference/operator/query/ne/
更多推荐
发布评论