23 Common Mongodb Operators & How to Use Them

23 Common Mongodb Operators & How to Use Them

In this article, we will take a look at the most commonly used query operators. We’ll explain what they do, then share examples so you can see how they work.

(This article is part of our MongoDB Guide. Use the right-hand menu to navigate.)

What are MongoDB operators?

MongoDB offers different types of operators that can be used to interact with the database. Operators are special symbols or keywords that inform a compiler or an interpreter to carry out mathematical or logical operations.

The query operators enhance the functionality of MongoDB by allowing developers to create complex queries to interact with data sets that match their applications.

MongoDB offers the following query operator types:

  • Comparison
  • Logical
  • Element
  • Evaluation
  • Geospatial
  • Array
  • Bitwise
  • Comments

MongoDB operators can be used with any supported MongoDB command.

Now, let’s look at commonly used operators. (We won’t touch on them all, there are so many.) We’ll use the following dataset with the find() function to demonstrate each operator’s functionality.

  • Database: supermarket
  • Collections: employees, inventory, payments, promo
use supermarket
db.employees.find()
db.inventory.find()
db.payments.find()
db.promo.find()

Dataset:

Comparison Operators

MongoDB comparison operators can be used to compare values in a document. The following table contains the common comparison operators.

Operator Description
$eq Matches values that are equal to the given value.
$gt Matches if values are greater than the given value.
$lt Matches if values are less than the given value.
$gte Matches if values are greater or equal to the given value.
$lte Matches if values are less or equal to the given value.
$in Matches any of the values in an array.
$ne Matches values that are not equal to the given value.
$nin Matches none of the values specified in an array.

$eq Operator

In this example, we retrieve the document with the exact _id value “LS0009100”.

db.inventory.find({"_id": { $eq: "LS0009100"}}).pretty()

Result:

$gt and $lt Operators

In this example, we retrieve the documents where the `quantity` is greater than 5000.

db.inventory.find({"quantity": { $gt: 5000}}).pretty()

Result:

Let’s find the documents with the ‘quantity’ less than 5000.

db.inventory.find({"quantity": { $lt: 5000}}).pretty()

Result:

$gte and $lte Operators

Find documents with ‘quantity’ greater than or equal to 5000.

db.inventory.find({"quantity": { $gte: 12000}}).pretty()

Result:

The following query returns documents where the quantity is less than or equal to 1000.

db.inventory.find({"quantity": { $lte: 1000}}).pretty()

Result:

$in and $nin Operators

The following query returns documents where the price field contains the given values.

db.inventory.find({"price": { $in: [3, 6]}}).pretty()

Result:

If you want to find documents where the price fields do not contain the given values, use the following query.

db.inventory.find({"price": { $nin: [5.23, 3, 6, 3.59, 4.95]}}).pretty()

Result:

$ne Operator

Find documents where the value of the price field is not equal to 5.23 in the inventory collection.

db.inventory.find({"price": { $ne: 5.23}})

Result:

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.