DEV Community

Ameni Ben Saada
Ameni Ben Saada

Posted on Edited on AI-assisted

MongoDB query operators:

Alt Text

When I started learning backend development, I discovered MongoDB: a document database that stores BSON documents with a flexible schema. Today, let’s walk through its basic CRUD methods and query operators.

So today we are going to learn how to write MongoDB queries such as:

CRUD Operations:

  • Create: insertOne(), insertMany()
  • Read: findOne(), find()
  • Update: updateOne(), updateMany()
  • Delete: deleteOne(), deleteMany()

More advanced query operations using:

  • Comparison query operators : $gt , $lt , $gte , $lte
  • Matching values and arrays: $in, $nin, $all

let's get started

Create operations:

Those Create or Insert operations give us the ability to add a new document to a collection. We can use:

  • insertOne: inserts a single document into a collection.
  • insertMany: inserts multiple documents into a collection.

Read Operations:

We use those operations to get data from our MongoDB database. We can use these methods:

  • find: This method is called on the collection you’d like to query.
  • findOne: This method is used to retrieve a single document from a collection.

Update Operations:

With Update operations, we can modify existing documents or documents in a collection. Let’s go through the update methods:

  • updateOne: This method is used to update a single document.
  • updateMany: This method is used to update multiple documents.

Delete Operations:

The deleteOne() and deleteMany() methods are used to remove documents from a collection. Let’s see how these methods work:

  • deleteOne: This method is used to delete a single document in a collection.
  • deleteMany: This method is used to delete multiple documents in a collection

Comparison Query Operators:

These are tools to locate the requested data in a database. So we use, $lt, $lte, $gt, and $gte set of operators to achieve this. Let’s look at them individually:

  • $gt: Match if the requested value is "greater than" the value provided in the query;
  • $gte: Match if the requested value is "greater than or equal to" the value provided in the query;
  • $lt: Match if the requested value is "less than" the value provided in the query;
  • $lte: Match if the requested value is "less than or equal to" the value provided in the query;

Matching Values and Arrays:

These operators include $in, $nin, and $all. These query operators take a list of one or more values. Let’s look at them individually:

  • $in: This operator will return a document if any of the given values matches the search field. If the field holds an array, the $in operator will return all the documents in a collection where at least one of the given values matches the search field.
  • $nin: $nin (not in) matches documents where the field contains none of the specified values, including documents where that field is missing. Combine it with $exists: true if the field must exist.
  • $all: $all matches an array field that contains all the specified values; their order does not matter.

I learned a lot during these few months, and I hope you did learn a thing or two from reading this.

Please let me know if there's anything wrong with this article. I would love to correct and improve it.

For more examples, see the official CRUD guide, $nin reference and $all reference.

Top comments (6)

Collapse
 
danielmmellop profile image
Daniel Mello •

Great article, waiting next.

Ps: You put "Delete: seleteOne()" instead of "Delete: deleteOne()"

Collapse
 
amenibensaada profile image
Ameni Ben Saada •

I just fixed it , Thank you Daniel

Collapse
 
meryemgh profile image
MeryemGh •

Thanks Ameni , that's a great article

Collapse
 
amenibensaada profile image
Ameni Ben Saada •

Thank youuuu

Collapse
 
bahachammakhi profile image
Baha chammakhi •

Great article ! Waiting for the next one.

Collapse
 
amenibensaada profile image
Ameni Ben Saada •

Thank youuu baha