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?