n33t
n33t

Reputation: 65

How to properly check if there are two model values in the object array in mongoose

I try to check if there are two keys and their values in the structure array, but I get no result, how can this be fixed?

Configuration:

[
  {
    id: 1,
    values: [
      {
        One: "One",
        Two: "Two",
        Three: "Three"
      }
    ]
  },
  {
    id: 2,
    values: [
      {
        One: "One",
        Two: "Two",
        Four: "Four"
      }
    ]
  }
]

Query

Model.find({
  "values": {
    $all: [
      {
        One: "One",
        Three: "Three",
      }
    ]
  }
})

Upvotes: 0

Views: 25

Answers (1)

J.F.
J.F.

Reputation: 15215

If I've understood correctly you can use $elemMatch to get desired value:

db.collection.find({
  "values": {
    $elemMatch: {
      One: "One",
      Three: "Three"
    }
  }
})

Example here.

Also another example with more values.

Upvotes: 1

Related Questions