CRUD Operations and Queries in MongoDB
1. Introduction
CRUD stands for:
-
Create
-
Read
-
Update
-
Delete
MongoDB performs CRUD operations on documents stored inside collections.
2. Create Operation (Insert)
Used to add new documents to a collection.
Commands
-
insertOne()– insert one document -
insertMany()– insert multiple documents
Example
3. Read Operation (Find)
Used to retrieve documents from a collection.
Commands
-
find()– fetch all matching documents -
findOne()– fetch a single document
Example
Query Operators
-
$gt– greater than -
$lt– less than -
$eq– equal
4. Update Operation
Used to modify existing documents.
Commands
-
updateOne() -
updateMany()
Update Operators
-
$set– update a field -
$inc– increment value
Example
5. Delete Operation
Used to remove documents from a collection.
Commands
-
deleteOne() -
deleteMany()
Example
6. Query Features in MongoDB
MongoDB provides powerful querying features:
Filtering
Sorting
Limiting Results
7. Aggregation (Basic Idea)
Used for data analysis and reporting.
Example
8. Advantages of MongoDB CRUD Operations
-
Simple and easy syntax
-
JSON-based queries
-
High performance
-
Flexible data handling
Comments
Post a Comment