0

I am trying this query but its return only one record.

db.getCollection('lists').find({_id:ObjectId("5b51c7cf307331187c5429bd"),_id:ObjectId("5b507c322fa5767930f0a09f")})

I also tried this but it returns nothing.

db.getCollection('lists').find({_id:ObjectId("5b51c7cf307331187c5429bd")},{_id:ObjectId("5b507c322fa5767930f0a09f")})
Hardik Shah
  • 3,726
  • 2
  • 18
  • 40
Raj Jaril
  • 258
  • 1
  • 14
  • Possible duplicate of [Mongo, find through list of ids](https://stackoverflow.com/questions/15102532/mongo-find-through-list-of-ids) – Ahmed Yousif Jul 25 '18 at 06:06

2 Answers2

1

There are multiple ways to find multiple records based on criteria.

$in:

db.getCollection('lists').find({_id: {"$in" : [ObjectId("5b51c7cf307331187c5429bd"), ObjectId("5b51c7cf307331187c5429bd")]}})

$or:

db.getCollection('lists').find({"$or" : [{ _id : ObjectId("5b51c7cf307331187c5429bd")}, { _id: ObjectId("5b51c7cf307331187c5429bd")}]})
Hardik Shah
  • 3,726
  • 2
  • 18
  • 40
1

for the multiple you should use $in operator of mongodb you can take the reference from mongodb dos

Vipul Pandey
  • 1,201
  • 10
  • 19