21

I have a db named student and I want to select the values between 30 and 60 from a column named u1. This is the query I used,but it shows some error

db.student.find($and:[{u1:{$gt:30}},{u1:{$lt:60}}])

3 Answers 3

62

try this:

db.student.find({ u1 : { $gt :  30, $lt : 60}});
Sign up to request clarification or add additional context in comments.

Comments

8

You could also try this:

db.students.find({$and:[{u1:{$gt:30}},{u1:{$lt:60}}]})

The above statement will display the value of the field u1 which are in between 30 and 60

1 Comment

For me, this syntax also works when interfacing MongoDB via mongo-express advanced query, while the accepted answer does not.
0

For all except the most simple queries, I find the aggregation pipeline gives you "more room to grow" and of course many more functions are available to you in the agg pipeline that cannot be used in $find:

db.students.aggregate([
    {$match: {$expr: {$and: [
        {$gt: ['$u1', 20]}
        ,{$lt: ['$u1', 23]}
    ] }
    }} 
])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.