如果我有这样的记录;
{
"text": "text goes here",
"words": ["text", "goes", "here"]
}
如何在 MongoDB 中匹配多个单词?当匹配单个单词时我可以这样做;
db.find({ words: "text" })
但是当我尝试对多个单词执行此操作时,它不起作用;
db.find({ words: ["text", "here"] })
我猜测通过使用数组,它会尝试将整个数组与记录中的数组进行匹配,而不是匹配单个内容。
取决于您是否要在何处查找文档words
包含两个元素 (text
and here
) using $all:
db.things.find({ words: { $all: ["text", "here"] }});
或其中任何一个 (text
or here
) using $in:
db.things.find({ words: { $in: ["text", "here"] }});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)