Mongo, find through list of ids

This works fine for me in Robo 3T. No need to create any object and just use the list of ids.

db.getCollection('my_collection').find({'_id':{$in:['aa37ba96']}})

// categoryId comma separated "5c875c27d131b755d7abed86,5c875b0ad131b755d7abed81" in request

var ids= req.body.categoryId.split(','); 

db.test.find({ categoryId: { $in: ids } });

After converting the strings into ObjectIds, you can use the $in operator to get the docs in the list. There isn't any query notation to get the docs back in the order of your list, but see here for some ways to handle that.

var ids = ['512d5793abb900bf3e20d012', '512d5793abb900bf3e20d011'];
var obj_ids = ids.map(function(id) { return ObjectId(id); });
db.test.find({_id: {$in: obj_ids}});

Tags:

Mongodb