Home > Mobile >  how to give 3 conditions in mongodb collection find()
how to give 3 conditions in mongodb collection find()

Time:10-29

db.mycollection.find({"$and": [{"key1": value1}, {"key2": value2}]})

This is for 2 filterd condtions

I also have key3 and value3. How can I query for a third condition?

CodePudding user response:

You don't need $and into find stage, you can add different keys in the object like this:

db.collection.find({
  "key1": 1,
  "key2": 2,
  "key3": 3
})

Example here.

By the way, to do it using $and is only to add one more object into the array, like this example

$and is an array with syntax:

{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

So you can add as many expressions as you want.

  • Related