Lud*_*son 456 mongoose mongodb
如果我有这个架构......
person = {
name : String,
favoriteFoods : Array
}
Run Code Online (Sandbox Code Playgroud)
... favoriteFoods
数组中填充了字符串.我怎样才能找到所有使用猫鼬"寿司"作为他们最喜欢的食物的人?
我希望有类似的东西:
PersonModel.find({ favoriteFoods : { $contains : "sushi" }, function(...) {...});
Run Code Online (Sandbox Code Playgroud)
(我知道$contains
在mongodb中没有,只是在了解解决方案之前解释我期望找到的内容)
Joh*_*yHK 636
作为favouriteFoods
一个简单的字符串数组,您可以直接查询该字段:
PersonModel.find({ favouriteFoods: "sushi" }, ...);
Run Code Online (Sandbox Code Playgroud)
但我还建议在模式中显式化字符串数组:
person = {
name : String,
favouriteFoods : [String]
}
Run Code Online (Sandbox Code Playgroud)
Ali*_*son 137
$contains
mongodb中没有操作员.
您可以使用JohnnyHK的答案.与mongo最接近的类比是$in
,使用此查询看起来像:
PersonModel.find({ favouriteFoods: { "$in" : ["sushi"]} }, ...);
Run Code Online (Sandbox Code Playgroud)
Pob*_*obe 83
$all
在这种情况下,我觉得更合适.如果您正在寻找寿司的人,您可以:
PersonModel.find({ favoriteFood : { $all : ["sushi"] }, ...})
Run Code Online (Sandbox Code Playgroud)
您可能希望过滤更多搜索,例如:
PersonModel.find({ favoriteFood : { $all : ["sushi", "bananas"] }, ...})
Run Code Online (Sandbox Code Playgroud)
$in
就像OR和$all
AND一样.检查一下:https://docs.mongodb.com/manual/reference/operator/query/all/
Kfi*_*rez 50
如果数组包含对象,例如if favouriteFoods
是以下对象的数组:
{
name: 'Sushi',
type: 'Japanese'
}
Run Code Online (Sandbox Code Playgroud)
您可以使用以下查询:
PersonModel.find({"favouriteFoods.name": "Sushi"});
Run Code Online (Sandbox Code Playgroud)
小智 32
如果你需要在子文档数组中找到包含NULL元素的文档,我发现这个查询非常有效:
db.collection.find({"keyWithArray":{$elemMatch:{"$in":[null], "$exists":true}}})
Run Code Online (Sandbox Code Playgroud)
此查询取自此帖子:具有空值的MongoDb查询数组
这是一个伟大的发现,它比我自己的初始和错误的版本(原来只适用于具有一个元素的数组)工作得更好:
.find({
'MyArrayOfSubDocuments': { $not: { $size: 0 } },
'MyArrayOfSubDocuments._id': { $exists: false }
})
Run Code Online (Sandbox Code Playgroud)
lookup_food_array 是数组。
match_stage["favoriteFoods"] = {'$elemMatch': {'$in': lookup_food_array}}
Run Code Online (Sandbox Code Playgroud)
lookup_food_array 的情况是字符串。
match_stage["favoriteFoods"] = {'$elemMatch': lookup_food_string}
Run Code Online (Sandbox Code Playgroud)
有一些方法可以实现这一目标。第一个是按$elemMatch
操作员:
const docs = await Documents.find({category: { $elemMatch: {$eq: 'yourCategory'} }});
// you may need to convert 'yourCategory' to ObjectId
Run Code Online (Sandbox Code Playgroud)
第二个是 by$in
或$all
运算符:
const docs = await Documents.find({category: { $in: [yourCategory] }});
Run Code Online (Sandbox Code Playgroud)
或者
const docs = await Documents.find({category: { $all: [yourCategory] }});
// you can give more categories with these two approaches
//and again you may need to convert yourCategory to ObjectId
Run Code Online (Sandbox Code Playgroud)
$in
类似于 OR 和$all
AND。有关更多详细信息,请检查此链接:https://docs.mongodb.com/manual/reference/operator/query/all/
第三个是按aggregate()
功能:
const docs = await Documents.aggregate([
{ $unwind: '$category' },
{ $match: { 'category': mongoose.Types.ObjectId(yourCategory) } }
]};
Run Code Online (Sandbox Code Playgroud)
使用aggregate(),您只能在类别数组中获得一个类别ID。
我从我的项目中获取了此代码片段,在这些项目中我必须找到具有特定类别的文档,因此您可以根据您的需求轻松自定义它。
归档时间: |
|
查看次数: |
433039 次 |
最近记录: |