Insert One – Insert Single Document in Collection.
syntax:- db.collection(<collection_name>).insertOne(<document>, <callback_function>)
Insert Many – Insert Multiple Document in Collection.
syntax:- db.collection(<collection_name>).insertMany(<documents_array>, <callback_function>)

Insert One -Insert Single Document in Collection

We have a database with the name of ‘mydb’ and we are going to insert a document in customers collection.

Insert single Document directly

//to import mongodb 
var MongoClient = require('mongodb').MongoClient;
//connect url
var url = "mongodb://localhost:27017/mydb";
//make client connect 
MongoClient.connect(url, function (err, client) {
    var db = client.db('mydb');
    if (err) throw err;
    // insert document to 'customers' collection using insertOne
    db.collection("customers").insertOne({ name: "ESI", address: "Highway 37" }, function (err, result) {
        if (err) throw err;
        console.log("1 document inserted");
        // close the connection to client when you are done with it
        client.close();
    });
});

Write this code in your editor and save with js extension and run this at your terminal.(i have saved above code with insert1.js)

Run Command

C:\Users\Your Name>node insert1.js

You can see the “1 document inserted!” message at console log.
Result

1 document inserted!

one document has been inserted into your ‘customers’ collection.)
(you can also use ‘insert’ instead of ‘insertOne’ ).

Insert using object

//to import mongodb 
var MongoClient = require('mongodb').MongoClient;
//connect url
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function (err, client) {
    var db = client.db('mydb');
    if (err) throw err;
    // document to be inserted
    var myObj = { name: " HEG", address: "Highway 12" };
    db.collection("customers").insertOne(myObj, function (err, result) {
        if (err) throw err;
        console.log("1 document inserted");
        // close the connection to client when you are done with it
        client.close();
    });
});

Write this code in your editor and save with js extension and run this at your terminal.
(i have saved above code with insert2.js)

Run Command

C:\Users\Your Name>node insert2.js

You can see the “1 document inserted!” message at console log.

Result

1 document inserted!

One document has been inserted into your ‘customers’ collection.)
(you can also use ‘insert’ instead of ‘insertOne’ ).

 

Insert Many – Insert Multiple Document in Collection

We have a database with the name of ‘mydb’ and We are going to insert multiple documents in ‘customers’ collection.

Insert multiple Documents directly

//to import mongodb 
var MongoClient = require('mongodb').MongoClient;
//connect url
var url = "mongodb://localhost:27017/mydb";
//make client connect 
MongoClient.connect(url, function (err, client) {
    var db = client.db('mydb');
    if (err) throw err;
    // insert multiple documents to 'customers' collection using insertMany
    db.collection("customers").insertMany([{ name: "ESI", address: "Highway 37" }, { name: "ABC", address: "Highway 15" }, { name: "INC", address: "Highway 18" }], function (err, result{
        if (err) throw err;
        console.log("3 documents inserted");
        client.close();
    });
});

Write this code in your editor and save with js extension and run this at your terminal.
(i have saved above code with insertmultiple1.js)

Run Command

C:\Users\Your Name>node insertmultiple1.js

You can see the “3 documents inserted” message at console log.

Result

3 documents inserted

three documents has been inserted into your ‘customers’ collection.)
(you can also use ‘insert’ instead of ‘insertMany’ ).

Insert multiple using object

//to import mongodb 
var MongoClient = require('mongodb').MongoClient;
//connect url
var url = "mongodb://localhost:27017/mydb";
//make client connect
MongoClient.connect(url, function(err, client) {
  var db = client.db('mydb');
  if (err) throw err;
  //documents to be inserted
  var myObj= [
    { name: "HEG", address: "Highway 12" },
    { name: "EIC", address: "Highway 19" },
    { name: "RSG", address: "Highway 22" },
    { name: "GTS", address: "Highway 2" }
  ];
   // insert documents to 'customers' collection using insertMany
db.collection("customers").insertMany(myObj, function(err, result) {
    if (err) throw err;
    console.log("4 documents inserted");
    client.close();
  });
});

Write this code in your editor and save with js extension and run this at your terminal.
(i have saved above code with insertmultiples2.js)

Run Command

C:\Users\Your Name>node insertmultiple2.js

You can see the “4 documents inserted” message at console log.

Result

4 documents inserted


Four documents has been inserted into your ‘customers’ collection.)
(you can also use ‘insert’ instead of ‘insertMany’ ).

When inserting a document into a collection it automatically gets a field ‘_id’ that contains a unique identifier for this document. This works as a primary key to uniquely identify each and every document.
but if you want to explicitly create an id field then you can add the id field when inserting the documents. like this-
{ _id:1, name: ” EIC”, address: “Highway 19” };

Get More Knowledge: