Node.js MongoDB - (Connectivity with Programming Languages)
MongoDB is a popular NoSQL database that works on the concept of collections and documents. It is a document-oriented database that stores and retrieves data in JSON-like documents. Node.js is a popular server-side JavaScript runtime built on the Google Chrome V8 JavaScript engine. Node.js provides an efficient way to build scalable network applications, and MongoDB provides high-performance, high-availability, and easy scalability for building modern web applications. In this page, we will discuss how to connect MongoDB with Node.js and perform CRUD operations.
Syntax
To connect MongoDB with Node.js, you need to install the MongoDB driver for Node.js. You can install the driver using the following NPM command:
npm install mongodb
After installing the MongoDB driver, you can use the following syntax to connect to a MongoDB database:
const MongoClient = require('mongodb').MongoClient;
MongoClient.connect(uri, options, function(err, client) {
// Perform operations here
client.close();
});
Here uri
is the URI of the MongoDB database you want to connect to, and options
is an optional object that specifies additional connection options. If the connection is successful, the callback function is called with two arguments: err
and client
. If there is an error connecting to the database, err
will be non-null; otherwise, client
will be a MongoDB client object that you can use to perform operations on the database.
Example
Here's an example of how to connect to a MongoDB database from Node.js and perform CRUD operations:
const MongoClient = require('mongodb').MongoClient;
const uri = 'mongodb://localhost:27017/mydb';
MongoClient.connect(uri, function(err, client) {
const collection = client.db("mydb").collection("mycollection");
// Insert a document
collection.insertOne({name: 'John Doe', age: 30}, function(err, result) {
console.log("Inserted document with ID " + result.insertedId);
// Update the document
collection.updateOne({name: 'John Doe'}, {$set: {age: 35}}, function(err, result) {
console.log("Updated " + result.modifiedCount + " document");
// Get a document
collection.findOne({name: 'John Doe'}, function(err, document) {
console.log("Found document: " + JSON.stringify(document));
// Delete the document
collection.deleteOne({name: 'John Doe'}, function(err, result) {
console.log("Deleted " + result.deletedCount + " document");
client.close();
});
});
});
});
});
Output
The output of the above code will be:
Inserted document with ID 5dd4575d61c3d8c8fbb1a432
Updated 1 document
Found document: {"_id":"5dd4575d61c3d8c8fbb1a432","name":"John Doe","age":35}
Deleted 1 document
Explanation
In the above example, we first connect to a MongoDB database using a MongoClient
object. We then retrieve a collection object by specifying the name of the database and collection we want to work with. We then perform a series of CRUD operations on the collection using the appropriate methods provided by the collection
object.
Use
Node.js is a popular runtime environment for building scalable network applications. MongoDB is a popular NoSQL database that provides high availability, easy scalability, and high performance for building modern web applications. By connecting MongoDB with Node.js, we can perform CRUD operations on the database using JavaScript code, which makes it easier to build web applications using a consistent programming language.
Important Points
- You need to install the MongoDB driver for Node.js using the NPM command
npm install mongodb
. - To connect to a MongoDB database from Node.js, you can use the
MongoClient
object provided by the MongoDB driver. - can perform CRUD operations on a MongoDB database by using the appropriate methods provided by the
collection
object.
Summary
In this page, we discussed how to connect MongoDB with Node.js and perform CRUD operations on the database. We covered the syntax, example, output, explanation, use, important points, and summary of connecting with Node.js. By following the example discussed in this page, you can connect to a MongoDB database from Node.js and perform CRUD operations on the database using JavaScript code.