How to query data between two values in MongoDB?

In MongoDB, you can use the $gt (greater than) and $lt (less than) operators to query data between two values.

For example, suppose we have a collection called students, which has a field named age, and we want to query for students whose age is between 20 and 30, we can use the following query statement:

db.students.find({ age: { $gt: 20, $lt: 30 } });

This will return students whose age is greater than 20 and less than 30. In the query statement, the $gt operator means greater than, and the $lt operator means less than.

bannerAds