1

Example of a document in a dummy collection.

{
    "_id": "1",
    "subjects": ['English', 'Maths', 'Science', 'French',..]
}

Now I want to select all the documents which are having any of the subjects mentioned in an array. Eg: ['Maths', 'French'].

So the result of the query should give all the documents wherever any of the subjects that are mentioned.

Therefore for the above examples, all the document's which are having Maths or French subjects should be returned.

1 Answer 1

1

You can use $in operator with find() or aggregate()

db.collection.find({
  "subjects": {
    $in: [
      "English"
    ]
  }
})

Or

db.collection.aggregate([
  {
    $match: {
      subjects: {
        $in: [
          "Maths",
          "English"
        ]
      }
    }
  }
])

Working Mongo playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.