88

I have an array of ids and I want to get all document of them at once. For that I am writing but it return 0 records.

How can I search using multiple Ids ?

db.getCollection('feed').find({"_id" : { "$in" : [  
"55880c251df42d0466919268","55bf528e69b70ae79be35006" ]}})

I am able to get records by passing single id like

db.getCollection('feed').find({"_id":ObjectId("55880c251df42d0466919268")})
6
  • 1
    You probably need to wrap the ids in ObjectId() like you do in the second example. Commented Aug 28, 2015 at 6:01
  • 12
    db.getCollection('feed').find({"_id" : { "$in" : [ ObjectId("55880c251df42d0466919268"),ObjectId("55bf528e69b70ae79be35006") ]}}) maybe works. Commented Aug 28, 2015 at 6:02
  • Thanks ... It is working Commented Aug 28, 2015 at 6:06
  • Pankaj, maybe you did not notice but on top of his comment, @kxxoling posted an answer. It is always good to mark the answer as "accepted" because it helps people having the same problem. They will see the green tick and be more confident it is a working solution. Also it gives a few points to kxxoling ;-) Commented Jul 20, 2020 at 8:39
  • How do we wrap individual ids in ObjectId in node? Commented May 6, 2021 at 4:42

7 Answers 7

88

MongoDB is type sensitive, which means 1 is different with '1', so are "55880c251df42d0466919268" and ObjectId("55880c251df42d0466919268"). The later one is in ObjectID type but not str, and also is the default _id type of MongoDB document.

You can find more information about ObjectID here.

Just try:

db.getCollection('feed').find({"_id" : {"$in" : [ObjectId("55880c251df42d0466919268"), ObjectId("55bf528e69b70ae79be35006")]}});
Sign up to request clarification or add additional context in comments.

Comments

39

I believe you are missing the ObjectId. Try this:

db.feed.find({ 
    _id: {
        $in: [ObjectId("55880c251df42d0466919268"), ObjectId("55bf528e69b70ae79be35006")]
    }
});

1 Comment

… or without ObjectID: you can still use $oid like: {"_id": {"$in": [{"$oid": "55880c251df42d0466919268"}, {"$oid": "55bf528e69b70ae79be35006"}]}}
12

For finding records of multiple documents you have to use "$in" operator Although your query is fine, you just need to add ObjectId while finding data for Ids

db.feed.find({
  "_id" : {
    "$in" : 
      [ObjectId("55880c251df42d0466919268"), 
       ObjectId("55bf528e69b70ae79be35006")
      ]
   }
});

2 Comments

Hi Vikas - your answer appears to be much the same as the two existing answers from kxxoling and Vimalraj Selvam, which both already emphasise that the ObjectId part is essential. Is there something extra that you want to contribute?
@VinceBowdren He provided more readable solution :)
1

Just I have use it, and is working fine:

module.exports.obtenerIncidencias386RangoDias = function (empresas, callback) {
    let arraySuc_Ids = empresas.map((empresa)=>empresa.sucursal_id)
            incidenciasModel.find({'sucursal_id':{$in:arraySuc_Ids}
            }).sort({created_at: 'desc'}).then(
                (resp)=>    {
                    callback(resp)}
            )
};

where:

empresas = ["5ccc642f9e789820146e9cb0","5ccc642f9e789820146e9bb9"]

Comments

1

The Id's need to be in this format : ObjectId("id").
you can use this to transform your string ID's :

const ObjectId = require('mongodb').ObjectId;

Comments

0

store array list in var and pass that array list to find function var list=db.collection_name.find() db.collection_name.find({_id:{$in:list}})

Comments

-4
db.getCollection({your_collection_name}).find({
    "_id" : {
    "$in" : 
      [({value1}), 
       ({value2})
      ]
   } 
    })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.