Match array elements/element (at least one) from another array in mongoose

Viewed 236

Lets say in my database :

{
  domains:[A, B, C],
  id: 1
},
{
  domains:[B, C],
  id: 2
},
{
  domains:[A],
  id: 3
},
{
  domains:[B, D],
  id: 4
}

I want to search query like domains: [A, D]. Basically i want to match at least one element not for all element.

My expecting answer is

{
  domains:[A, B, C],
  id: 1
},
{
  domains:[A],
  id: 3
},
{
  domains:[B, D],
  id: 4
}

Cause A and D(at least one) are found into this objects.

I want to retrieve questions, which matches the at least one topics?

1 Answers

You can use $in operator in this way:

yourModel.find({
  "domains": {
    "$in": [
      "A",
      "D"
    ]
  }
})

Example here

Related