【问题标题】:mongodb find by multiple array itemsmongodb通过多个数组项查找
【发布时间】:2011-12-30 00:56:15
【问题描述】:

如果我有这样的记录;

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

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

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

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

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

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

【问题讨论】:

    标签: arrays search mongodb


    【解决方案1】:

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

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

    或其中任何一个(texthere)使用$in

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

    【讨论】:

    猜你喜欢
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 2022-12-05
    • 1970-01-01
    相关资源
    最近更新 更多