I have a list of ID's and I want to query a collection based on that list. The field I'm filtering is id_, it is an ObjectID.
Example:
list = ['abcd','mnop','qrst']
I want to search documents that the _id is in that list:
cursor = db.find( {"_id": { "$in": list} } )
cursor = db.find( {"_id": { "$in": ObjectId(list)} } )
I tried both options above and none of them worked.
The first one returned empty, and the second one throws a type error:
TypeError: id must be an instance of (bytes, str, ObjectId), not <class 'list'>
How can I correct my code?
As @prasad_ said, it is needed to generate the ObjectId from the string.
list_ = ['abcd','mnop','qrst']
list_ = [ObjectId(x) for x in list_]
cursor = db.find( {"_id": { "$in": list_} } )
So the "$in" operator of MongoDB refers to the list that has ObjectId's as its content.