Node JS - Node js MongoDB Insert Record - MongoDB - Node - Node JS tutorial - webnode




How to Insert Record of MongoDB in Node.js?

  • The insert One method is used to insert record in MongoDB's collection.
  • The first argument of the insert One method is an object which contains the name and value of each field in the record you want to insert.
  • learn nodejs - node-js tutorial - node js mongodb insert sql - nodejs examples -  nodejs programs
Related nodejs article tags - node js - node js tutorial - node js examples

Insert Single record

  • To insert a record into a table in MongoDB, we use the insertOne() method.
  • The first parameter of the insertOne() method is an object containing the name(s) and value(s) of each field in the record you want to insert.
  • It also takes a callback function where you can work with any errors, or the result of the insertion
 node-js mongodb insertdata

Learn Node js - node js Tutorial - node-js mongodb insertdata - node - Node js Examples

Example

  • Insert a record in "employees" collection.
  • Create a js file named "insert.js", having the following code:

  • insert.js:

    var MongoClient = require('mongodb').MongoClient;  
    var url = "mongodb://localhost:27017/ MongoDatabase";  
    MongoClient.connect(url, function(err, db) {  
    if (err) throw err;  
    var myobj = { name: "Ajeet Kumar", age: "28", address: "Delhi" };  
    db.collection("employees").insertOne(myobj, function(err, res) {  
    if (err) throw err;  
    console.log("1 record inserted");  
    db.close();  
    });  
    });  
    

    Open the command terminal and run the following command: Node insert.js

    Output:

     procedure of insert record in node.js

    Learn Node js - node js Tutorial - Now a record is inserted in the collection. - node - Node js Examples

    Insert Multiple Records

    • To insert multiple records into a table in MongoDB, we use the insertMany() method. The first parameter of the insertMany() method is an array of objects, containing the data you want to insert. It also takes a callback function where you can work with any errors, or the result of the insertion:
      • You can insert multiple records in a collection by using insert() method. The insert() method uses array of objects which contain the data you want to insert.
    learn nodejs - node-js tutorial - node js - mongodb insert command - nodejs examples -  nodejs programs

    Example

    • Insert multiple records in the collection named "employees".
    • Create a js file name insertall.js, having the following code:

    insertall.js

    var MongoClient = require('mongodb').MongoClient;  
    var url = "mongodb://localhost:27017/ MongoDatabase";  
    MongoClient.connect(url, function(err, db) {  
    if (err) throw err;  
    var myobj = [     
    { name: "Mahesh Sharma", age: "25", address: "Ghaziabad"},  
    { name: "Tom Moody", age: "31", address: "CA"},  
    { name: "Zahira Wasim", age: "19", address: "Islamabad"},  
    { name: "Juck Ross", age: "45", address: "London"}  
    ];  
    db.collection("customers").insert(myobj, function(err, res) {  
    if (err) throw err;  
    console.log("Number of records inserted: " + res.insertedCount);  
    db.close();  
    });  
    });  
    

    Open the command terminal and run the following command: Node insertall.js

    Output:

     procedure of multiple record insertion in node.js

    Learn Node js - node js Tutorial - procedure of multiple record insertion in node.js - node - Node js Examples

    You can see here 4 records are inserted.


    This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

    Related Searches to Node.js MongoDB Insert Record