David Spira
David Spira

Reputation: 153

how you would write this pymongo/mongodb query, in a way it inclues strings inside an array? (image included)

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

Edit

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

Answers (1)

JoshuaCS
JoshuaCS

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

Related Questions