When working with data, we may find ourselves in a situation where we need to compare values. For example, to retrieve records where some field value is greater or less than a given number. In this topic, we will learn about comparison operators in MongoDB and how to use them.
Comparison operators
From the school Math course, you may remember some basic comparison operators, such as >, <, = and so on. In MongoDB, they are interpreted a little differently. They start with a dollar sign ($), which is followed by an abbreviation for what the operator does. By the operator's syntax you can easily understand what it stands for.
Here is the table of the MongoDB comparison operators:
| Operator | Description |
|---|---|
$eq |
Equal to a specified value. |
$gt |
Greater than a specified value. |
$gte |
Greater than or equal to a specified value. |
$in |
Checks if the value is specified in an array. |
$lt |
Less than a specified value. |
$lte |
Less than or equal to a specified value. |
$ne |
Not equal to a specified value. |
$nin |
Checks if the value is not specified in an array. |
For a more detailed understanding, let's take a look at the examples of how the comparison operators are used.
Equal and not-equal comparison operators
For all of our examples, we will take just one collection, which is hotels. Take a look at the data this collection stores:
{name: "John's Hotel", rooms: 20, price_per_night: 30},
{name: "Europe Plaza", rooms: 50, price_per_night: 120},
{name: "Sierra Motel", rooms: 15, price_per_night: 25}
As you can notice, it is a pretty simple collection, just to illustrate the examples. We will review the examples of operators comparing them with a find() function. But you can also use comparison operators when you need to update or delete data.
First of all, let's talk about an equality operator - $eq. Specifying the $eq operator is equivalent to using the {field: <value>}. Let's get information about the John's Hotel using the equality operator:
db.hotels.find({name: {$eq: "John's Hotel"}})
We received the information we wanted in the form of a single record:
{name: "John's Hotel", rooms: 20, price_per_night: 30}
The $ne operator, on the contrary, selects records in which the field value is not equal to the specified value. We will get hotel records with a price per night not equal to 120:
db.hotels.find({price_per_night: {$ne: 120}})
We will get the expected result:
{name: "John's Hotel", rooms: 20, price_per_night: 30},
{name: "Sierra Motel", rooms: 15, price_per_night: 25}Greater-than and less-than comparison operators
Now let's talk about $gt, $gte, $lt and $lte operators. They compare the value of the field with some value that you specified. In our next example, we will try to get all hotels with 20 or more than rooms:
db.hotels.find({rooms: {$gte: 20}})
The result will contain John's Hotel and Europe Plaza only:
{name: "John's Hotel", rooms: 20, price_per_night: 30},
{name: "Europe Plaza", rooms: 50, price_per_night: 120}
You can also use multiple comparison operators simultaneously. For instance, let's write a query where the number of rooms is more than 10 and the price per night is less than 50:
db.hotels.find({rooms: {$gt: 10}, price_per_night: {$lt: 50}})
We will get information on these two hotels:
{name: "John's Hotel", rooms: 20, price_per_night: 30},
{name: "Sierra Motel", rooms: 15, price_per_night: 25}Using comparison operators with arrays
You can also use multiple values for comparison using arrays. Array is a data structure that stores multiple values. For example, like a box in that you can put some things: ["book", "ball", "toy"].
MongoDB has $in and $nin operators that check whether the field value is in the array you specified or not. Let's run through the examples.
In this case, we need to retrieve records of hotels that have the price per night 25 or 30. We will store these prices in an array like this: [25, 30]. And we will compare the value of the price_per_night field with this array.
db.hotels.find({price_per_night: {$in: [25, 30]}})
This command will give us the records about two hotels: John's Hotel and Sierra Motel as well.
On the other hand, $nin operator checks if the field value is not in an array. Let's execute our previous command with the same numbers but using $nin operator:
db.hotels.find({price_per_night: {$nin: [25, 30]}})
In this case, the result will contain only one document, where the price is neither 25 nor 30:
{name: "Europe Plaza", rooms: 50, price_per_night: 120}
Conclusion
In this topic, you learned about the comparison operators and how to use them. Now you know about equal, not-equal, greater-than and less-than comparison operators, and how to work with arrays when comparing values.
Take another look at the table of operators and start practicing!