热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

查找包含特定值的数组的文档

本文翻译自:FinddocumentwitharraythatcontainsaspecificvalueIfIhavethisschema如果我有这个架构..

本文翻译自:Find document with array that contains a specific value

If I have this schema... 如果我有这个架构...

person = {name : String,favoriteFoods : Array
}

... where the favoriteFoods array is populated with strings. ...在字符串中填充favoriteFoods数组。 How can I find all persons that have "sushi" as their favorite food using mongoose? 我怎样才能找到所有以猫鼬为食的人?

I was hoping for something along the lines of: 我一直希望以下方面的东西:

PersonModel.find({ favoriteFoods : { $contains : "sushi" }, function(...) {...});

(I know that there is no $contains in mongodb, just explaining what I was expecting to find before knowing the solution) (我知道mongodb中没有$contains ,只是在知道解决方案之前先解释了我期望找到的内容)




#1楼

参考:https://stackoom.com/question/1E9AM/查找包含特定值的数组的文档




#2楼

As favouriteFoods is a simple array of strings, you can just query that field directly: 由于favouriteFoods是一个简单的字符串数组,因此您可以直接查询该字段:

PersonModel.find({ favouriteFoods: "sushi" }, ...);

But I'd also recommend making the string array explicit in your schema: 但我也建议您在架构中使字符串数组显式:

person = {name : String,favouriteFoods : [String]
}

The relevant documentation can be found here: https://docs.mongodb.com/manual/tutorial/query-arrays/ 相关文档可以在这里找到: https : //docs.mongodb.com/manual/tutorial/query-arrays/




#3楼

There is no $contains operator in mongodb. mongodb中没有$contains运算符。

You can use the answer from JohnnyHK as that works. 您可以使用JohnnyHK的答案,因为它可以起作用。 The closest analogy to contains that mongo has is $in , using this your query would look like: 包含mongo的最接近类推是$in ,使用此查询,您的查询将类似于:

PersonModel.find({ favouriteFoods: { "$in" : ["sushi"]} }, ...);



#4楼

I know this topic is old, but for future people who could wonder the same question, another incredibly inefficient solution could be to do: 我知道这个话题很老,但是对于将来可能会想知道相同问题的人们,另一种效率极低的解决方案是:

PersonModel.find({$where : 'this.favouriteFoods.indexOf("sushi") != -1'});

This avoids all optimisations by MongoDB so do not use in production code. 这避免了MongoDB的所有优化,因此请勿在生产代码中使用。




#5楼

In case you need to find documents which contain NULL elements inside an array of sub-documents, I've found this query which works pretty well: 如果您需要在子文档数组中查找包含NULL元素的文档,我发现此查询非常有效:

db.collection.find({"keyWithArray":{$elemMatch:{"$in":[null], "$exists":true}}})

This query is taken from this post: MongoDb query array with null values 该查询摘自该帖子: 具有空值的MongoDb查询数组

It was a great find and it works much better than my own initial and wrong version (which turned out to work fine only for arrays with one element): 这是一个很好的发现,它比我自己的初始版本和错误版本要好得多(原来的版本只适用于具有一个元素的数组):

.find({'MyArrayOfSubDocuments': { $not: { $size: 0 } },'MyArrayOfSubDocuments._id': { $exists: false }
})



#6楼

I feel like $all would be more appropriate in this situation. 我觉得$all在这种情况下会更合适。 If you are looking for person that is into sushi you do : 如果您正在寻找喜欢寿司的人,可以这样做:

PersonModel.find({ favoriteFood : { $all : ["sushi"] }, ...})

As you might want to filter more your search, like so : 您可能希望过滤更多搜索内容,例如:

PersonModel.find({ favoriteFood : { $all : ["sushi", "bananas"] }, ...})

$in is like OR and $all like AND. $in就像OR, $all像AND。 Check this : https://docs.mongodb.com/manual/reference/operator/query/all/ 检查一下: https : //docs.mongodb.com/manual/reference/operator/query/all/


推荐阅读
author-avatar
kf_zjk
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有