Example insert database in mongoDB with nodejs

Insert Into Collection

To insert a record, or document as it is called in MongoDB, into a collection, 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 document you want to insert.

It also takes a callback function where you can work with any errors, or the result of the insertion:

Example 1:

 

var MongoClient = require('mongodb').MongoClient;

var url = "mongodb://192.168.1.122:27017/";

MongoClient.connect(url, function(err, db) {

  if (err) throw err;

  var dbo = db.db("mydb");

  var data = { name: "codewr.com", address: "Highway 37" };

  dbo.collection("customers").insertOne(data, function(err, res) {

    if (err) throw err;

    console.log("1 document inserted");

    db.close();

  });

});

 

Run:

 

C:\ Nodejs>node mongodbInsertOneData.js

1 document inserted!

 

Insert Multiple Documents

To insert multiple documents into a collection 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:

Example 2: 

 

var MongoClient = require('mongodb').MongoClient;

var url = "mongodb://192.168.1.122:27017/";

MongoClient.connect(url, function(err, db) {

  if (err) throw err;

  var dbo = db.db("mydb");

  var dataInsert = [

    { name: 'name1', address: 'Highway 71'},

    { name: 'name2', address: 'Lowstreet 4'},

    { name: 'name3', address: 'Apple st 652'},

    { name: 'name4', address: 'Mountain 21'},

    { name: 'name5', address: 'Valley 345'},

    { name: 'name6', address: 'Ocean blvd 2'},

    { name: 'name7', address: 'Green Grass 1'},

    { name: 'name8', address: 'Sky st 331'},

    { name: 'name9', address: 'One way 98'},

    { name: 'name10', address: 'Yellow Garden 2'},

    { name: 'name11', address: 'Park Lane 38'},

    { name: 'name12', address: 'Central st 954'},

    { name: 'name13', address: 'Main Road 989'},

    { name: 'name14', address: 'Sideway 1633'}

  ];

  dbo.collection("customers").insertMany(dataInsert, function(err, res) {

    if (err) throw err;

    console.log("Number of documents inserted: " + res.insertedCount);

    db.close();

  });

});

 

Run:

 

C:\Nodejs>node mongodbInsertMultiData.js

Number of documents inserted: 14

{ result: { ok: 1, n: 14 },

  ops:

   [ { name: 'name1',

       address: 'Highway 71',

       _id: 5ad9cbc8e19c2120bc96abd4 },

     { name: 'name2',

       address: 'Lowstreet 4',

       _id: 5ad9cbc8e19c2120bc96abd5 },

     { name: 'name3',

       address: 'Apple st 652',

       _id: 5ad9cbc8e19c2120bc96abd6 },

     { name: 'name4',

       address: 'Mountain 21',

       _id: 5ad9cbc8e19c2120bc96abd7 },

     { name: 'name5',

       address: 'Valley 345',

       _id: 5ad9cbc8e19c2120bc96abd8 },

     { name: 'name6',

       address: 'Ocean blvd 2',

       _id: 5ad9cbc8e19c2120bc96abd9 },

     { name: 'name7',

       address: 'Green Grass 1',

       _id: 5ad9cbc8e19c2120bc96abda },

     { name: 'name8',

       address: 'Sky st 331',

       _id: 5ad9cbc8e19c2120bc96abdb },

     { name: 'name9',

       address: 'One way 98',

       _id: 5ad9cbc8e19c2120bc96abdc },

     { name: 'name10',

       address: 'Yellow Garden 2',

       _id: 5ad9cbc8e19c2120bc96abdd },

     { name: 'name11',

       address: 'Park Lane 38',

       _id: 5ad9cbc8e19c2120bc96abde },

     { name: 'name12',

       address: 'Central st 954',

       _id: 5ad9cbc8e19c2120bc96abdf },

     { name: 'name13',

       address: 'Main Road 989',

       _id: 5ad9cbc8e19c2120bc96abe0 },

     { name: 'name14',

       address: 'Sideway 1633',

       _id: 5ad9cbc8e19c2120bc96abe1 } ],

  insertedCount: 14,

  insertedIds:

   { '0': 5ad9cbc8e19c2120bc96abd4,

     '1': 5ad9cbc8e19c2120bc96abd5,

     '2': 5ad9cbc8e19c2120bc96abd6,

     '3': 5ad9cbc8e19c2120bc96abd7,

     '4': 5ad9cbc8e19c2120bc96abd8,

     '5': 5ad9cbc8e19c2120bc96abd9,

     '6': 5ad9cbc8e19c2120bc96abda,

     '7': 5ad9cbc8e19c2120bc96abdb,

     '8': 5ad9cbc8e19c2120bc96abdc,

     '9': 5ad9cbc8e19c2120bc96abdd,

     '10': 5ad9cbc8e19c2120bc96abde,

     '11': 5ad9cbc8e19c2120bc96abdf,

     '12': 5ad9cbc8e19c2120bc96abe0,

     '13': 5ad9cbc8e19c2120bc96abe1 } }