Mongodb在一个字段上查询取决于另一个字段

时间:2019-12-06 07:19:33

标签: mongodb

我在mongodb中有一个集合。这是链接https://mongoplayground.net/p/aDMwOc5Wal4。 我需要所有的狗,橘子和红色蔬菜。

我正在尝试查询但没有找到方法。

1 个答案:

答案 0 :(得分:2)

您可以尝试一下。

db.collection.find({
  $or: [
    {
      "type": {
        "$in": [
          "Dog",
          "Orange"
        ]
      }
    },
    {
      $and: [
        {
          "color": {
            "$in": [
              "red"
            ]
          },
          "cat": {
            "$in": [
              "Vegetable"
            ]
          }
        }
      ]
    }
  ]
})