【问题标题】:Mongo db : Query on array that matches any elemet of another passed arrayMongodb:查询与另一个传递数组的任何元素匹配的数组
【发布时间】:2020-07-03 15:13:57
【问题描述】:

有如下文件:

{ _id:1, color:['red', 'green', 'yellow'},
{ _id:2, color:['red', 'blue', 'orange'},
{ _id:3, color:['blue', 'black', 'grey'}

现在我想做的是: 如果我通过了['red']

它应该返回 id 1 和 2 的文档。如果我通过了 ['black', 'yellow'],它应该返回 id 1 和 3 的文档。

【问题讨论】:

标签: arrays mongodb mongodb-query


【解决方案1】:

你可以使用$elemMatch来得到你想要的。

试试这个:

db.collection.find({
  color: {
    $elemMatch: {
      $in: [
        "black",
        "yellow"
      ]
    }
  }
})

看看这个Mongo Playground demo,使用查询。

【讨论】:

  • 如果对您有帮助,请也给答案投票! @sudip-bolakhe
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 1970-01-01
  • 2016-05-28
  • 2021-05-06
  • 1970-01-01
  • 2017-02-10
相关资源
最近更新 更多