MongoDB bulk insert for documents


For bulk insert, you can use insert() in MongoDB. Let us create a collection with documents −

> var manyDocument = db.demo255.initializeUnorderedBulkOp();
> manyDocument.insert( { "Name":"Chris",Age:24} );
> manyDocument.insert( {"Name":"Bob",Age:22 } );
> manyDocument.insert( { "Name":"David",Age:23 } );
> manyDocument.execute();
BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 3,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})

Display all documents from a collection with the help of find() method −

> db.demo255.find();

This will produce the following output −

{ "_id" : ObjectId("5e47a25e1627c0c63e7dba82"), "Name" : "Chris", "Age" : 24 }
{ "_id" : ObjectId("5e47a25e1627c0c63e7dba83"), "Name" : "Bob", "Age" : 22 }
{ "_id" : ObjectId("5e47a25e1627c0c63e7dba84"), "Name" : "David", "Age" : 23 }

Updated on: 31-Mar-2020

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements