繁体   English   中英

mongodb通过多个数组项查找

[英]mongodb find by multiple array items

如果我有这样的记录;

{
  "text": "text goes here",
  "words": ["text", "goes", "here"]
}

如何在MongoDB中匹配多个单词? 当匹配一个单词时,我可以做到;

db.find({ words: "text" })

但是当我尝试多个单词时,它不起作用;

db.find({ words: ["text", "here"] })

我猜想通过使用数组,它会尝试将整个数组与记录中的数组进行匹配,而不是匹配单个内容。

取决于您是否要尝试使用$all查找words包含两个元素( texthere )的文档:

db.things.find({ words: { $all: ["text", "here"] }});

或使用$in之一( texthere ):

db.things.find({ words: { $in: ["text", "here"] }});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM