Reputation: 153
i wanna make a query looking for all documents that have, in their respective havelists, say, string1, string3, string4
https://i.sstatic.net/LvwfB.png
I made some progress, i found that if i make this query
j = USERS_COLLECTION.find({ 'havelist.0.1': card}, {'havelist':1, 'position':1,'_id':1})
it works, but the issue is that i might eventually have to search on havelist.0.1, havelist.1.1, havelist.2.1, ..., havelist.n.1
Upvotes: 0
Views: 29
Reputation: 2624
Please, David, try this:
users = USERS_COLLECTION.find({{'havelist': {'$elemMatch': { '1': card }}}}, {'havelist':1, 'position':1,'_id':1})
Such query uses $elemMatch
query operator, which matches documents that contain an array field with at least one element that matches all the specified query criteria.
Here is the official documentation of $elemMatch
operator of MongoDB
Let me know if this worked, otherwise, tell me what went wrong.
Upvotes: 1