skip to Main Content

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.

2

Answers


  1. 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))
    }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search