MongoDB CRUD Operations in Node.js
MongoDB CRUD Operations in Node.js Explained
MongoDB CRUD operations in Node.js are used to manage data in backend applications. CRUD stands for Create, Read, Update, and Delete. Understanding MongoDB CRUD operations in Node.js is important because every backend application needs to store and manage data efficiently.
Setup MongoDB in Node.js
Install MongoDB Driver
npm install mongodb
Basic Connection Code
const { MongoClient } = require(‘mongodb’);
const url = ‘mongodb://localhost:27017’;
const client = new MongoClient(url);
async function connectDB() {
await client.connect();
console.log(‘Database connected’);
}
connectDB();
Create Operation in MongoDB
Insert One Document
const db = client.db(‘test’);
const users = db.collection(‘users’);
await users.insertOne({ name: ‘Amit’, age: 25 });
Insert Multiple Documents
await users.insertMany([
{ name: ‘Rahul’, age: 24 },
{ name: ‘Neha’, age: 22 }
]);
Create operation is used to add new data into MongoDB.
Read Operation in MongoDB
Find All Documents
const data = await users.find().toArray();
console.log(data);
Find One Document
const user = await users.findOne({ name: ‘Amit’ });
Read operation is used to fetch data from the database.
Update Operation in MongoDB
Update One Document
await users.updateOne(
{ name: ‘Amit’ },
{ $set: { age: 26 } }
);
Update Multiple Documents
await users.updateMany(
{ age: { $lt: 25 } },
{ $set: { status: ‘young’ } }
);
Update operation is used to modify existing data.
Delete Operation in MongoDB
Delete One Document
await users.deleteOne({ name: ‘Amit’ });
Delete Multiple Documents
await users.deleteMany({ age: { $lt: 23 } });
Delete operation is used to remove data from MongoDB.
Complete CRUD Example in Node.js
const { MongoClient } = require(‘mongodb’);
const client = new MongoClient(‘mongodb://localhost:27017’);
async function run() {
await client.connect();
const db = client.db(‘test’);
const users = db.collection(‘users’);
await users.insertOne({ name: ‘Test User’, age: 20 });
const allUsers = await users.find().toArray();
console.log(allUsers);
await users.updateOne({ name: ‘Test User’ }, { $set: { age: 21 } });
await users.deleteOne({ name: ‘Test User’ });
}
run();
Best Practices for MongoDB CRUD Operations
Use Async Await
Always use async await for database operations.
Handle Errors
Wrap database calls inside try catch blocks.
Use Indexing
Use indexes for faster query performance.
Real-World Use of MongoDB CRUD
MongoDB CRUD operations in Node.js are used in applications like user management systems, e-commerce platforms, and dashboards. These operations form the core of backend development.
FAQs on MongoDB CRUD Operations
What is CRUD in MongoDB
CRUD stands for Create, Read, Update, and Delete operations.
How to perform CRUD in Node.js
You can use MongoDB driver methods like insertOne, find, updateOne, and deleteOne.
Is MongoDB good for backend development
Yes, MongoDB is widely used for backend development with Node.js.
Continue Learning Backend Development
Now that you understand MongoDB CRUD operations in Node.js, you are ready to move to the next lesson where you will learn about connecting MongoDB with Express.js using Mongoose. To explore more structured tutorials and courses, click here for more free courses.



