Comparison Query Operators in MongoDB
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
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.
