1

How to get all documents with a find query where the queried array fields are exists in the document?

{
    name: 'test1',
    "array": [
        {
            "name": "100",
            "subArray": [
                "arr1"
            ]
        },
        {
            "name": "200",
            "subArray": [
                "arr2",
                "arr3"
            ]
        }
    ]
}

If i use this query in the find({}) it gives back not only the good results:

$and: [
  {'array.name': '100'}, {'array.subArray': 'arr1'},
  {'array.name': '200'}, {'array.subArray': 'arr2'}
]

1 Answer 1

1

You need to use the $elemMatch operator. Your query should look like this:

$and: [
  { array: { $elemMatch: { name: '100', subArray: 'arr1' } } },
  { array: { $elemMatch: { name: '200', subArray: 'arr1' } } }
]
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.