MongoDB bulk insert for documents

MongoDBDatabaseBig Data Analytics

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 }
raja
Published on 31-Mar-2020 11:44:14
Advertisements