mongodb mongodb按多个数组项查找
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8145523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mongodb find by multiple array items
提问by Stephen Belanger
If I have a record like this;
如果我有这样的记录;
{
"text": "text goes here",
"words": ["text", "goes", "here"]
}
How can I match multiple words from it in MongoDB? When matching a single word I can do this;
如何在 MongoDB 中匹配多个单词?匹配单个单词时,我可以这样做;
db.find({ words: "text" })
But when I try this for multiple words, it doesn't work;
但是当我尝试多个单词时,它不起作用;
db.find({ words: ["text", "here"] })
I'm guessing that by using an array, it tries to match the entire array against the one in the record, rather than matching the individual contents.
我猜测通过使用数组,它会尝试将整个数组与记录中的数组进行匹配,而不是匹配单个内容。
回答by
Depends on whether you're trying to find documents where words
contains both elements (text
and here
) using $all
:
取决于您是否尝试使用以下方法查找words
同时包含元素 (text
和here
) 的文档$all
:
db.things.find({ words: { $all: ["text", "here"] }});
or either of them (text
or here
) using $in
:
或其中任何一个(text
或here
)使用$in
:
db.things.find({ words: { $in: ["text", "here"] }});