1

I have database like;

ObjectID(234567654rtfgjytrfgfgf){
       _id : "3456ytgfewrtyhgfd"
       Categories:{
             [0] : "Apple"
             [1] : "Mango"
       }
}
ObjectID(23dfghjthrgytrfgfgf){
       _id : "67tghjgfdrthg"
       Categories:{
             [0] : "Orange"
             [1] : "Mango"
       }
}
ObjectID(23dfghjthrgytrfgfgf){
       _id : "67tghjgfdrthg"
       Categories:{
             [0] : "Apple"
             [1] : "Mango"
       }
}

I want to find the document that has the Apple at a 0th index of category array in Mongo database document using python. I am trying in this way;

cursor = collection.find({"Categories" : "Apple"})
        for document in cursor:
            print(document) 

It is printing the same result but how can I specify the index of Categories array? I am not specifying the categories index in collection.find({"Categories" : "Apple"})

1 Answer 1

1

to match only index 0:

cursor = collection.find({"Categories.0" : "Apple"})
for document in cursor:
    print(document) 
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.