mongodbHow to use the "between" operator in MongoDB?
The $between
operator in MongoDB is used to specify a range of values for a given field. It is used to select documents where the value of the field is greater than or equal to the specified lower bound and less than or equal to the specified upper bound.
Example
db.collection.find({
field: {
$between: [lowerBound, upperBound]
}
})
Output example
{ "_id" : ObjectId("5f3d7f9f8f9f9f9f9f9f9f9f"), "field" : 10 }
{ "_id" : ObjectId("5f3d7f9f8f9f9f9f9f9f9f9f"), "field" : 15 }
{ "_id" : ObjectId("5f3d7f9f8f9f9f9f9f9f9f9f"), "field" : 20 }
Code explanation
db.collection.find()
: This is the method used to query the collection.field
: This is the field in the collection that is being queried.$between
: This is the operator used to specify a range of values for the given field.lowerBound
: This is the lower bound of the range of values.upperBound
: This is the upper bound of the range of values.
Helpful links
More of Mongodb
- How to use triggers in MongoDB?
- How to use MongoDB queue?
- How to use watch in MongoDB?
- How to use the limit operator in MongoDB?
- How to use unwind in MongoDB?
- How to list MongoDB users?
- How to perform a health check for MongoDB?
- How to check the version of MongoDB?
- How to implement pagination in MongoDB?
- How to remove a field from MongoDB?
See more codes...