How to use multiple query on mongodb using pymongo?

Viewed 79

I have a collection on mongodb, which has 1000 object like this one:

{
"_id":3,
"list": ["item1","item2", "item3"]
}

And i have a python list of 10 item. And i want to check any object from the collection has values from the list or not.

I've tried to do this:

from pymongo import MongoClient

mylist = ["item4","item5", "item6", "item1"]
database_uri = "mongodb_uri"
client = MongoClient(database_uri)

collection = client["database"]["collection"]
exists = []
for i in mylist:
    data = collection.find_one({"list":i})
    if data:
        exists.append(i)
print (exists)

result:

['item1']

But it takes a long time to complete. I wanted to do this in a single query. How can i do this?

1 Answers

First, if possible, create a multikey index on the embedded list:

db.collection.createIndex({"list":1})

This will add a lot of storage, but it will improve your query response times a lot.

Next, you can do the same thing in a single query use $or or $in. I'm not 100% sure of the syntax, but I think this should work:

exists = set([])
results = collection.find({"list":{"$elemMatch":{"$in":mylist}}})
for result in results:
  exists.update(result.list)

The downside of this is that you will send all records that matches any of the items to your python client, and let python do the extraction and sorting out.

You can also do the extraction and sorting server side, and only send the aggregated list of matched items to the python client, by using a mongo aggregation pipeline, but that's slightly more complicated.

Related