In this tutorial you will learn about the Nodejs MongoDB Insert Record and its application with practical example.
Node.js MongoDB Insert Record
A record, or document can be inserted into MongoDB collection using the insertOne() method. It takes an object containing keys(s) and value(s) of each field in the document as first parameter, and a callback function as second parameter where you can work with any errors, or the result of the insertion.
Example :- Let’s insert a record or document in “employees” collection.
Step 1:- Let’s, create a node_mongo_insert.js file and put the following code in it –
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/nodemongo"; MongoClient.connect(url, function(err, db) { if (err) throw err; var emps = { emp_name: "Keith", emp_age: "28", emp_salary: "10000" }; db.collection("employees").insertOne(emps, function(err, res) { if (err) throw err; console.log("W3Adda | Single record inserted"); db.close(); }); }); |
Step 2:- Save the code and open the terminal again, and type the following command in order to run the file.
1 |
$ node node_mongo_insert.js |
you will see following output on terminal –
Output:-
1 |
W3Adda | Single record inserted |
Insert Multiple Records
The insertMany() method is used to insert multiple records in a MongoDB collection. It takes array of objects that contain the records or documents you want to insert.
Step 1:- Let’s, create a node_mongo_multi_insert.js file and put the following code in it –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/nodemongo"; MongoClient.connect(url, function(err, db) { if (err) throw err; var emps = [ { emp_name: "Alex", emp_age: "25", emp_salary: "5000"}, { emp_name: "John", emp_age: "35", emp_salary: "8000"}, { emp_name: "Steve", emp_age: "28", emp_salary: "7500"}, { emp_name: "Scott", emp_age: "40", emp_salary: "10000"} ]; db.collection("employees").insertMany(emps, function(err, res) { if (err) throw err; console.log("W3Adda | Insert Multiple Records"); console.log("Number of records inserted: " + res.insertedCount); db.close(); }); }); |
Step 2:- Save the code and open the terminal again, and type the following command in order to run the file.
1 |
$ node node_mongo_multi_insert.js |
you will see following output on terminal –
Output:-
1 2 |
W3Adda | Insert Multiple Records Number of records inserted: 4 |
The Result Object
insertMany() method, returns a result object which holds the information about how the insertion affected the database.
Example:- The insertedCount property returns the number of document inserted
1 |
console.log(res.insertedCount) |
Output:-
1 |
4 |
The _id Field
The _id Field is used to assign an unique id to each of the document or records in a collection. If you don’t specifies it the MongoDB engine will assign a unique id for each of the document. You can also specify it and it must be unique for each of the document.