MongoDB is a popular NoSQL database system that provides a flexible and scalable way of storing and managing data. It is commonly used with Node.js to build web applications. In this blog post, we’ll provide a step-by-step guide to performing CRUD operations in MongoDB using Node.js with clear examples.
Step 1: Installing MongoDB Driver for Node.js
The first step is to install the MongoDB driver for Node.js. The driver is a module that allows Node.js to interact with the MongoDB database. Using below command we can install mongo drive with our application,
1 |
npm install mongodb |
Creating a MongoDB database
Before we connect, we need to create a MongoDB database that we can use to store our data. We can do this by creating a new database and collection using the mongo command-line tool:
1 2 3 |
mongo > use mongoCrudDemoDB > db.createCollection('users') |
This will create a new database called mongoCrudDemoDBand a new collection called users.
Step 2: Creating a Connection to MongoDB
To interact with the MongoDB database, we need to create a connection to it. We can use the MongoClient class provided by the MongoDB driver to create a connection. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
const { MongoClient } = require('mongodb'); const uri = 'mongodb://localhost:27017/mongoCrudDemoDB'; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); client.connect((err) => { if (err) { console.log(err); return; } console.log('Connected to MongoDB'); // perform CRUD operations here client.close(); }); |
In this example, we’re creating a connection to a MongoDB instance running on the localhost with the database name “mongoCrudDemoDB”. We’re using the useNewUrlParser and useUnifiedTopology options to ensure that we’re using the latest version of the driver.
Step 3: Performing CRUD Operations in MongoDB using Node.js
Once we have a connection to the MongoDB database, we can perform CRUD operations on it. Here are some examples:
Create Operation
To create a new document in MongoDB using Node.js, we can use the insertOne() method. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
const { MongoClient } = require('mongodb'); const uri = 'mongodb://localhost:27017/mongoCrudDemoDB'; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); client.connect((err) => { if (err) { console.log(err); return; } const db = client.db('mongoCrudDemoDB'); db.collection('users').insertOne({ name: 'Learn Infinity', age: 30 }, (err, result) => { if (err) { console.log(err); return; } console.log('Document inserted'); }); client.close(); }); |
In this example, we’re inserting a new document into a collection called “users”. The insertOne() method takes two arguments:
the document to insert and a callback function that is called once the operation is complete.
Read Operation
To read data from MongoDB using Node.js, we use the find() method. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
const { MongoClient } = require('mongodb'); const uri = 'mongodb://localhost:27017/mongoCrudDemoDB'; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); client.connect((err) => { if (err) { console.log(err); return; } const db = client.db('mongoCrudDemoDB'); if (err) { console.log(err); return; } console.log(result); }); client.close(); }); |
In this example, we’re searching for all documents in the “users” collection that have a “name” field with the value “Learn Infinity”. The find() method returns a cursor, which we can convert to an array using the toArray() method. The result is an array of all documents that match the query.
Update Operation
To update a document in MongoDB using Node.js, we can use the updateOne() method. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
const { MongoClient } = require('mongodb'); const uri = 'mongodb://localhost:27017/mongoCrudDemoDB'; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); client.connect((err) => { if (err) { console.log(err); return; } const db = client.db('mongoCrudDemoDB'); db.collection('users').updateOne( { $set: { age: 31 } }, (err, result) => { if (err) { console.log(err); return; } console.log('Document updated'); } ); client.close(); }); |
In this example, we’re updating the age of the document in the “users” collection that has a “name” field with the value “Learn Infinity”. Here updating new value to “age” filed using $set operator.
Delete Operation
To delete a document from MongoDB using Node.js, we can use the deleteOne() method. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
const { MongoClient } = require('mongodb'); const uri = 'mongodb://localhost:27017/mongoCrudDemoDB'; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); client.connect((err) => { if (err) { console.log(err); return; } const db = client.db('mongoCrudDemoDB'); if (err) { console.log(err); return; } console.log('Document deleted'); }); client.close(); }); |
In this example, we’re deleting the document from the “users” collection that has a “name” field with the value “Learn Infinity”.
Conclusion
In this blog post, we’ve provided a step-by-step guide to performing CRUD operations in MongoDB using Node.js. We’ve covered how to install the MongoDB driver for Node.js, how to create a connection to MongoDB, and how to perform create, read, update, and delete operations in MongoDB using Node.js. We hope that this blog post has been helpful in getting you started with using MongoDB with Node.js for building web applications.