Mongo DB query operators:

This past few months, I have spent time learning the back-end technologies and how they are working. In this journey, I discovered Mongo DB,which is flexible and allows developers to quickly store large amounts of unstructured data.

So today we are…


This content originally appeared on DEV Community and was authored by Ameni Ben Saada

Alt Text

This past few months, I have spent time learning the back-end technologies and how they are working. In this journey, I discovered Mongo DB,which is flexible and allows developers to quickly store large amounts of unstructured data.

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

CRUD Operations:

  • Create: insertOne(), insertMany()
  • Read: findOne(), find()
  • Update: updateOne(), update()
  • Delete: seleteOne(), deleteMany()

More advanced query operations using:

  • Comparison query operators : $gt , $lt , $gte , $lte
  • Set Operators: $in, $all, $nin

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 Mongo DB 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.

Consider the following:

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;

Set Operators:

Set operators includes $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) returns a document only when none of the given elements matches the search field.
  • $all: $all matches if all the given elements match the search key.

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.


This content originally appeared on DEV Community and was authored by Ameni Ben Saada


Print Share Comment Cite Upload Translate Updates
APA

Ameni Ben Saada | Sciencx (2021-06-26T19:45:31+00:00) Mongo DB query operators:. Retrieved from https://www.scien.cx/2021/06/26/mongo-db-query-operators/

MLA
" » Mongo DB query operators:." Ameni Ben Saada | Sciencx - Saturday June 26, 2021, https://www.scien.cx/2021/06/26/mongo-db-query-operators/
HARVARD
Ameni Ben Saada | Sciencx Saturday June 26, 2021 » Mongo DB query operators:., viewed ,<https://www.scien.cx/2021/06/26/mongo-db-query-operators/>
VANCOUVER
Ameni Ben Saada | Sciencx - » Mongo DB query operators:. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/26/mongo-db-query-operators/
CHICAGO
" » Mongo DB query operators:." Ameni Ben Saada | Sciencx - Accessed . https://www.scien.cx/2021/06/26/mongo-db-query-operators/
IEEE
" » Mongo DB query operators:." Ameni Ben Saada | Sciencx [Online]. Available: https://www.scien.cx/2021/06/26/mongo-db-query-operators/. [Accessed: ]
rf:citation
» Mongo DB query operators: | Ameni Ben Saada | Sciencx | https://www.scien.cx/2021/06/26/mongo-db-query-operators/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.