MongoDB is a NoSQL database that can store a large amount of unstructured data. Data stored in MongoDB are easy to retrieve as it can hold them in flexible formats depending upon the need such as graphs, key values, documents, etc.
A MongoDB database has many collections in which we can insert records, the records in MongoDB are called documents.
Also read: NodeJS MongoDB Select
Inserting Documents in a MongoDB Collection using NodeJS
For using the MongoDB database in NodeJS it is required to use the MongoDB module, which provides a MongoClient which then has a method connect() to connect to the database, inside the callback of this method we get the database object which is used to select a collection using collection() method, when we are done selecting a collection then use one of the methods of inserting record according to the requirement to insert the document in the respective collection.
Methods to Insert Document using NodeJS
In NodeJS to insert documents in a collection of a MongoDB database, we have two methods insertMany() and insertOne().
Using the insertOne() Method
This method is used to insert a single record in a collection. It takes a document and a callback as an argument. Inside the callback, it returns an error if occurs and a response object contains the information regarding the record inserted.
database.collection(collectionName).insertOne(doc, function(err, res) {
// callback body
});
where,
- database is a database object,
- collectionName is the name of a collection,
- doc is a record to be inserted
Example:
Below is an example to insert a single record in a collection.
const { MongoClient } = require('mongodb');
MongoClient.connect("mongodb://localhost:27017/", function (err, client) {
if (err) throw err;
const database = client.db("newdatabase");
const doc = { name: "Aditya", email: "[email protected]" };
database.collection("newcollection").insertOne(doc, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
});
});
Run the Code:
To run the above code, create a project folder, and copy and paste it into a JavaScrip file “app.js”.
Then locate the folder in the terminal and execute the below command to install the MongoDB module.
npm i mongodb
Then execute the below command to run the code of the “app.js” file.
node app.js
Output:
Using the insertMany() Method
This method is used to insert multiple records in a collection. It takes an array of multiple records and a callback as an argument. Inside the callback, it returns an error if it occurs and a response object contains the information regarding the records inserted.
database.collection(collectionName).insertMany(docs, function(err, res) {
// callback body
});
where,
- database is a database object,
- collectionName is the name of a collection,
- docs is an array of records to be inserted
Example:
Below is an example to insert multiple records in a collection.
const { MongoClient } = require('mongodb');
MongoClient.connect("mongodb://localhost:27017/", function (err, client) {
if (err) throw err;
const database = client.db("newdatabase");
const docs = [{ name: "Rack", email: "[email protected]" }, { name: "Rahul", email: "[email protected]" }];
database.collection("newcollection").insertMany(docs, function(err, res) {
if (err) throw err;
console.log("No. of records inserted: " + res.insertedCount);
});
});
Run the Code:
To run the above code, create a project folder, and copy and paste it into a JavaScrip file “app.js”.
Then locate the folder in the terminal and execute the below command to install the MongoDB module.
npm i mongodb
Then execute the below command to run the code of the “app.js” file.
node app.js
Output:
Summary
Records are inserted into a collection of MongoDB databases in NodeJS using the insertMany() and insertOne() methods. insertMany() method can insert multiple records whereas insertOne() method can only insert a single record. In order to use these methods it is required first to select the collection in which the records are required to be inserted. Hope this tutorial helps you to understand the process of inserting records in a collection of MongoDB databases using NodeJS.
Reference
https://www.mongodb.com/docs/drivers/node/current/usage-examples/insertOne/
https://www.mongodb.com/docs/drivers/node/current/usage-examples/insertMany/