Home > Back-end >  How to find all elements of array in database
How to find all elements of array in database

Time:06-10

I had taken this array

arr = ["1","2"]

And I want to find this id's are present in mongodb in user collection or not, So how do I write query in mongoose for that?

user.find({'empid':'arr'},(err,res))

I am doing like this.

CodePudding user response:

You should first loop through your array then run mongodb query to find item. code should be some thing like this:

for(item in arr){
    user.find({'empid':item},(err,res))
}

CodePudding user response:

I think you are looking for the query operator "$in".

Here's how you can do it.

db.user.find({
  "empid": {"$in": ["1", "2"] }
})

Try it on mongoplayground.net.

  • Related