1

I have a document as:

{
    'name':'XYZ',
    'address':'Street 21',
    'phone': {
        'home': [123456, 456123]
    },
    'qualification': {
        'primary': [
            ['AB','75'],
            ['CD','80'],
            ['EF','50']
        ]
    }
}

Simple query would be like:

n = collection.find({'name':'XYZ'})

I can fetch documents using a list on the 'phone.home' subdocument key like this:

list1 = [123456]
n = collection.find({'phone.home':{'$in': list1}})

I am stuck on how to query the collection using the qualification key. Suppose I have either 'AB' or '75' (only one of them at a time), I am not able to make up query, e.g I only have the following list1:

list1 = ['AB']

n = collection.find({'qualification.primary':{'$in':list1}})

This definitely is not giving me the desired result. Any guidance will be appreciated!

1 Answer 1

3

Use $elemMatch two times query as :

db.collectionName.find(
    {
      "qualification.primary":{ "$elemMatch": { "$elemMatch":{ "$in": ["AB"] }}}
    },
    { "qualification.primary.$":1 }
)
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.