Search code examples
mongodbpymongoobject-identity

$all parameter in mongodb does not work with ObjectId list


I retrieve a list of ObjectId and I want to retrieve all object in my mongo database using the parameter $all

I'm using pymongo and my request look like this :

db.database.collection.find({ "_id" : { "$all" : [ObjectId('4ee371837c93dd33dc000003'),ObjectId('4eef9f647c93dd1a90000000')] } })

but the cursor count returned by the request is 0 but when I do this request:

db.database.collection.find_one({ "_id" : ObjectId('4ee371837c93dd33dc000003')})

It returns me the good object

Anyone know why it does not work?


Solution

  • That query does not make sense. You are asking for the unique and single-valued _id field to have all of two distinct values at the same time.

    I think you want $in:

    db.database.collection.find({ "_id" : { 
       "$in" : 
         [ObjectId('4ee371837c93dd33dc000003'),
          ObjectId('4eef9f647c93dd1a90000000')] } })