How to do a range query in Mongoose? How to do a range query in Mongoose? mongoose mongoose

How to do a range query in Mongoose?


Your solution of having a min and max price makes good scene

Have a look at the mongo query comparisons here.

db.test.find({minNum : { $gte :  50},maxNum : { $lte :  100}});

https://docs.mongodb.com/manual/reference/operator/query-comparison/


What if your querying range within a price(maxPrice and minPrice) and what if user not only provided maxPrice or minPrice or what if user doesn't provide any price

        price: { $lte: maxPrice || 1000000000, $gte: minPrice || 0 }
  • If maxPrice is null maxPrice default value will be 100 crores

  • if minPrice is null minPrice default value will be 0

This answer not pretty related to the question but someone who searching a condition like this might suit them.


db.testCollection.find({       lowVal : {$gte : 50},       highVal : {$lte : 100}});