2

Suppose I have documents like this:

{
    "_id", "1",
    "myarray": [
        {
           "address": "abc",
           "role": "input",
        }
        {
           "address": "def",
           "role": "output",
        }
    ]
},
{
    "_id", "2",
    "myarray": [
        {
           "address": "xyz",
           "role": "input",
        }
        {
           "address": "abc",
           "role": "output",
        }
    ]
}

I want to return documents where myarray.address is abc and myarray.role is output, but not the documents where exists myarray.address='abc' and exists myarray.role='output', but the documents where exists element of myarray array:

address: "abc",
role: "output"

Using the example above, I want only the document with _id=2.

1 Answer 1

2

You can use $elemMatch query operator to match multiple fields inside an array

db.collection.find({ myArray: { $elemMatch: { address: 'abc', role: 'output' }}})
Sign up to request clarification or add additional context in comments.

1 Comment

So simple :) My position at MongoDB learning curve seems 1 step from the beginning :)

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.