如果我有这样的记录;
{
"text": "text goes here",
"words": ["text", "goes", "here"]
}
如何在 MongoDB 中匹配多个单词?当匹配一个单词时,我可以这样做;
db.find({ words: "text" })
但是当我对多个单词尝试这个时,它不起作用;
db.find({ words: ["text", "here"] })
我猜测通过使用数组,它会尝试将整个数组与记录中的数组进行匹配,而不是匹配单个内容。
最佳答案
取决于您是否尝试使用 $all
查找 words
包含两个元素(text
和 here
)的文档:
db.things.find({ words: { $all: ["text", "here"] }});
或其中任何一个(text
或 here
)使用 $in
:
db.things.find({ words: { $in: ["text", "here"] }});
https://stackoverflow.com/questions/8145523/
相关文章:
mongodb - 为什么 Mongoose 既有模式又有模型?
macos - 无法连接到mongodb errno :61 Connection refused
mongodb - 未指定排序顺序时,MongoDB 如何对记录进行排序?
javascript - 在NodeJS中获取Mongo数据库中插入文档的_id
mongodb - GridFS 对于生产来说是否足够快速和可靠?
node.js - 如何获得 Mongoose 模型的所有计数?
node.js - ( Node :3341) DeprecationWarning: Mongoo
python - 类型错误 : ObjectId ('' ) is not JSON seriali