BhaskerYadav
BhaskerYadav

Reputation: 589

Mongodb query on a field depending on another field

I have a collection in mongodb. Here is the link https://mongoplayground.net/p/aDMwOc5Wal4. I need all the Dogs, Oranges and Red colored vegetables.

I am trying a query but not getting the approach.

Upvotes: 2

Views: 77

Answers (1)

Serkan Arslan
Serkan Arslan

Reputation: 13393

you can try this.

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

Upvotes: 2

Related Questions