Mongodb Query giving error: Expression $gt takes exactly 2 arguments. 1 were passed in

Viewed 2577

I am writting the following query to retrive the records based on 2 conditions.Executing the Query giving me error:

> db.employee.find({age:{$lt:32}},{salary:{$lt:40000}})

Here is the error Message:

Error: error: {
        "ok" : 0,
        "errmsg" : "Expression $lt takes exactly 2 arguments. 1 were passed in.",
        "code" : 16020,
        "codeName" : "Location16020"
}
2 Answers

To do a find query based in two conditions, you need to do in the same query object.

Note that you are doing:

find({obj1},{obj2}

But second parameter into find method is for projection (docs)

So you need this query:

db.collection.find({
  "age": {
    "$lt": 32
  },
  "salary": {
    "$lt": 40000
  }
})

Note how there is only one object with two fields: age and salary.
Something like find( { age:{}, salary:{}} ).

Example here

It seems you want to implement AND condition. You have to write all the conditions in the single object clause {} and separate the conditions by a comma. Like:

db.employee.find({ age:{$lt:32}, salary:{$lt:40000}});
Related