19
MongoDB 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 MongoDB,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: deleteOne(), 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 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.
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.
19