Search code examples
node.jsmongodbmongoose

How to find from multiple values in mongodb


There is a collection in MongoDB like:

{
    "_id" : ObjectId("5bcc4c7d1e2c701be024de99"),
    "className" : "Class 10",
    "gradeLevel" : "10th",
    "subjectName" : "Math",
    "teacherId" : "[email protected]",
    "__v" : 0
},
{
    "_id" : ObjectId("5bd45277f5b9430013f4a604"),
    "className" : "Class 11",
    "gradeLevel" : "11th",
    "subjectName" : "Maths",
    "teacherId" : "[email protected]",
    "__v" : 0
},
{
    "_id" : ObjectId("5bd470fe452cb33af4b8407a"),
    "className" : "Class 10",
    "gradeLevel" : "12th",
    "subjectName" : "Math",
    "teacherId" : "[email protected]",
    "__v" : 0
},

Now I want to fetch those documents with values of_id: ObjectId("5bd45277f5b9430013f4a604"), ObjectId("5bd470fe452cb33af4b8407a").

I am using mongoose and node.js. So please tell me is there any way to do this.


Solution

  • Model.find({ _id: { $in: ['5bd45277f5b9430013f4a604', '5bd470fe452cb33af4b8407a'] } })
      .then(docs => console.log(docs))
      .catch(err => console.log(err));