An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
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 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 youd 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. Lets 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:
ThedeleteOne()anddeleteMany()methods are used to remove documents from a collection. Lets 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$gteset of operators to achieve this. Lets 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. Lets 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$inoperator 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:$allmatches 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.
Original Link: https://dev.to/amenibensaada/mongo-db-query-operators-3fcn
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To