Introduction

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.

Query Operators

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.

Comparison Operators at a Glance
Using `$gt`, `$gte`, `$lt`, `$lte`

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:

use comic_book_store_db

db.comic_books.findOne(
   { published_year: { $gt: 2000 } },
   { title: 1, published_year: 1, _id: 0 }
)

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:

use comic_book_store_db

db.comic_books.findOne(
   { published_year: { $lte: 2010 } },
   { title: 1, published_year: 1, _id: 0 }
)
`$eq` in Action

Let's demonstrate the $eq (equals) operator. Suppose we want to find a comic book published in 1963:

use comic_book_store_db

db.comic_books.findOne(
   { published_year: { $eq: 1963 } },
   { title: 1, published_year: 1, _id: 0 }
)

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.

`$ne` in Action

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:

use comic_book_store_db

db.comic_books.findOne(
   { issue_number: { $ne: 1 } },
   { title: 1, issue_number: 1, _id: 0 }
)

The $ne operator filters out any records where issue_number equals 1, and MongoDB returns a comic where issue_number is not 1.

Summary
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal