Insert a document using NodeJs and mongoDB
Insert a document using Node.js and mongoDB
Suppose you have a mongoDB database for courses and you want to insert a record into the database using Node.js.
First step is to install mongoDB driver for Node.js If it is not already installed. Please open command line and run following command.
> npm install mongodb
This will install mongoDB driver for node js. This command will install mongoDB driver for Node.js. After successfully installation we move to NodeJs part of our script.
Create a script named insert.js. and then add following code
var MongoClient = require('mongodb').MongoClient; MongoClient.connect('mongodb://localhost:27017/course', function(err, db){ if(err) throw err; var doc = {'student':'Calvin', 'age': 16}; db.collection('students').insert(doc, function(err, inserted){ if(err) throw err; console.dir("Successfully inserted: " + JSON.stringify(inserted)); return db.close(); }); });
Here is explanation of the code above.
1. Include mongoDB module
var MongoClient = require('mongodb').MongoClient;
Above line is used to include mongoDB module to use in script.
2. Connect and Insert a document to MongoDB
The code line below is using connect method of MongoClient, Inside connect method a connection string is passed with port number 27017 , and suppose the database or collection name is course. second parameter is a callback function that will be called after connection is made to the database and it receives the parameters err and db.
MongoClient.connect('mongodb://localhost:27017/course',function(err, db){
While making a connection to database server if there is some error the line below will throw an error.
if(err) throw err;
If there is no error then we create a new document in an object named doc with following values.
var doc = {'student':'Calvin', 'age': 16};
The line below executes a command to insert the doc object into students table. For this purpose we use db object’s collection method and pass ‘students’ table as parameter.
After collection method we have called insert method and pass it two parameters. First parameter is doc object and second parameter is a callback function that has two parameters. First parameter to callback is err while second parameter is inserted . Inserted are the docs that are inserted into database.
db.collection('students').insert(doc, function(err, inserted){ if(err) throw err;
if docs are successfully inserted the following code will run.
console.dir("Successfully inserted: " + JSON.stringify(inserted)); return db.close();
We will see message “Successfully inserted”. and then db object is closed.
In command prompt use this command to run the above script.
> node insert.js
After sucessfull query execution we wil se the message
'Successfully inserted: [{"student":"Calvin","age":16,"_id":"54cd3b48471f18c820c643d8"}]'
In mongoDB shell in course database if we run the command.
db.students.find();
we will get the result.
{ "_id" : ObjectId("54cd3b48471f18c820c643d8"), "student" : "Calvin", "age" : 16 }
In next tutorials we will explore advance topics in Node.js and MongoDB.
Nice article. Please share more Detailed articles
Thanks Ramesh. Keep in touch. you will see lot more good content