Implement MongoDB CRUD Operations

This commit is contained in:
rupeshbangar
2026-03-24 14:51:42 +05:30
parent 5494c8c3c5
commit 64330437c0

111
Week-05/Day_04/README.md Normal file
View File

@@ -0,0 +1,111 @@
# MongoDB CRUD Operations
## Introduction
The performing **CRUD** (Create, Read, Update, Delete) operations in MongoDB. This is the heart of any database-driven application.
---
## Create (Insert Data)
Insert a single document:
```js
db.students.insertOne({
name: "Rupesh",
age: 23,
course: "MCA"
});
```
Insert multiple documents:
```js
db.students.insertMany([
{ name: "Amit", age: 22 },
{ name: "Sneha", age: 21 }
]);
```
---
## Read (Fetch Data)
Get all documents:
```js
db.students.find();
```
Get specific data:
```js
db.students.find({ name: "Rupesh" });
```
Pretty format:
```js
db.students.find().pretty();
```
---
## Update Data
Update one document:
```js
db.students.updateOne(
{ name: "Rupesh" },
{ $set: { age: 24 } }
);
```
Update multiple:
```js
db.students.updateMany(
{ course: "MCA" },
{ $set: { status: "active" } }
);
```
---
## Delete Data
Delete one document:
```js
db.students.deleteOne({ name: "Rupesh" });
```
Delete many:
```js
db.students.deleteMany({ age: { $lt: 23 } });
```
---
## Query Operators
```js
db.students.find({ age: { $gt: 20 } });
```
Operators:
* `$gt` → greater than
* `$lt` → less than
* `$eq` → equal
---
## Indexing (Basic)
```js
db.students.createIndex({ name: 1 });
```
Improves query performance.