In the last lesson, you delved into the concept of projection. You did an outstanding job understanding how to retrieve specific fields from documents! This lesson will be the first in a series focusing on Query Operators in MongoDB. These operators are special keys that we use to search documents in a collection. This lesson will introduce the first category of Query Operators — Comparison Query Operators. These operators allow us to perform comparison queries on numerical, string, and date values within our database. They are highly powerful and help us efficiently find specific documents that match certain criteria.
MongoDB provides a variety of query operators that allow us to perform complex searches and queries on our data. These operators can be categorized into several groups:
- Comparison Operators: Used for comparing values.
- Logical Operators: Used for combining multiple query conditions.
- Element Operators: Used for matching fields that have specific properties.
- Evaluation Operators: Used for performing evaluations on our fields.
- Geospatial Operators: Used for geospatial queries.
- Bitwise Operators: Used for performing bitwise operations.
- Miscellaneous Operators: Various other operators that don't fit into the above categories.
In this course, we will cover everything except evaluation, geospatial, bitwise, and miscellaneous operators. Today's lesson focuses on comparison operators.
There are six comparison query operators in MongoDB that we can use:
$eq
(equals): Matches values that are equal to a specified value.$gt
(greater than): Matches values that are greater than a specified value.$gte
(greater than or equal to): Matches values that are greater than or equal to a specified value.$lt
(less than): Matches values that are less than a specified value.$lte
(less than or equal to): Matches values that are less than or equal to a specified value.$ne
(not equal to): Matches all values that are not equal to the specified value.
Additionally, we have $in
and $nin
for matching any of the values specified in an array, but we'll cover these in the next lesson where we talk about arrays.
Let's see how to use these comparison query operators. For demonstration let's use our beloved comic_book_store_db
database again. Imagine you want to find a comic book that was published after the year 2000. Here is how you can do it:
JavaScript1use comic_book_store_db 2 3db.comic_books.findOne( 4 { published_year: { $gt: 2000 } }, 5 { title: 1, published_year: 1, _id: 0 } 6)
In this example, { published_year: { $gt: 2000 } }
is our query filter. This means "find a record where published_year
is greater than 2000." The { title: 1, published_year: 1, _id: 0 }
part is our projection.
Here's another example where we utilize $lte
(less than or equal to) operator to find a comic published on or before 2010:
JavaScript1use comic_book_store_db 2 3db.comic_books.findOne( 4 { published_year: { $lte: 2010 } }, 5 { title: 1, published_year: 1, _id: 0 } 6)
Let's demonstrate the $eq
(equals) operator. Suppose we want to find a comic book published in 1963:
JavaScript1use comic_book_store_db 2 3db.comic_books.findOne( 4 { published_year: { $eq: 1963 } }, 5 { title: 1, published_year: 1, _id: 0 } 6)
While you can also write the query more simply as { published_year: 1963 }
, the $eq
operator becomes particularly useful in complex queries where you might combine multiple conditions and operators.
Sometimes, we might want to exclude a specific value from our search. We use the $ne
(not equal to) operator for this. For instance, let's find a comic book where the issue_number
is not 1:
JavaScript1use comic_book_store_db 2 3db.comic_books.findOne( 4 { issue_number: { $ne: 1 } }, 5 { title: 1, issue_number: 1, _id: 0 } 6)
The $ne
operator filters out any records where issue_number
equals 1, and MongoDB returns a comic where issue_number
is not 1.
Great job reaching the end of this lesson! We learned about comparison query operators in MongoDB. We explored how to use $gt
, $gte
, $lt
, $lte
, $eq
, and $ne
to find specific documents in our data. These operators will be very useful in helping us query large volumes of diverse data efficiently.
In the next practice section, you'll apply what you learned by solving various problems using comparison query operators. Keep the examples from today's lesson in mind. You're doing a fantastic job, and I look forward to seeing you in the next lesson!