{"id":792,"date":"2022-07-11T18:10:44","date_gmt":"2023-03-28T22:57:34","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/uncategorized\/using-mongodbs-insertmany-function-allows-for-bulk-insertion-into-a-mongodb-database\/"},"modified":"2024-03-05T14:22:51","modified_gmt":"2024-03-05T14:22:51","slug":"insertmany-function-for-bulk-insertion","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/","title":{"rendered":"insertMany function for bulk insertion into a MongoDB database."},"content":{"rendered":"<p>Today, we will explore the concept of MongoDB bulk insert. In MongoDB, it is possible to insert multiple documents simultaneously using the bulk insert operation. This operation involves passing an array of documents as a parameter to the insert method.<\/p>\n<h2>One possibility for rephrasing the statement could be: &#8220;Implementing batch insert in <a href=\"https:\/\/www.mongodb.com\/\">MongoDB<\/a>.&#8221;<\/h2>\n<p>By default, MongoDB bulk insert performs insertion in a specific order. If an error occurs during the insertion at any point, the remaining documents are not inserted. Let&#8217;s take a look at an example of how to insert multiple documents using MongoDB bulk insert via the command line.<\/p>\n<h3>Insert multiple documents into MongoDB.<\/h3>\n<pre class=\"post-pre\"><code>\r\n&gt; db.car.insert(\r\n... [\r\n... { _id:1,name:\"Audi\",color:\"Red\",cno:\"H101\",mfdcountry:\"Germany\",speed:75 },\r\n... { _id:2,name:\"Swift\",color:\"Black\",cno:\"H102\",mfdcountry:\"Italy\",speed:60 },\r\n\r\n... { _id:3,name:\"Maruthi800\",color:\"Blue\",cno:\"H103\",mfdcountry:\"India\",speed:70 },\r\n... { _id:4,name:\"Polo\",color:\"White\",cno:\"H104\",mfdcountry:\"Japan\",speed:65 },\r\n... { _id:5,name:\"Volkswagen\",color:\"JetBlue\",cno:\"H105\",mfdcountry:\"Rome\",speed:80 }       \r\n...  ]\r\n...  )\r\nBulkWriteResult({\r\n\t\"writeErrors\" : [ ],\r\n\t\"writeConcernErrors\" : [ ],\r\n\t\"nInserted\" : 5,\r\n\t\"nUpserted\" : 0,\r\n\t\"nMatched\" : 0,\r\n\t\"nModified\" : 0,\r\n\t\"nRemoved\" : 0,\r\n\t\"upserted\" : [ ]\r\n})\r\n<\/code><\/pre>\n<p>Five documents were inserted during this operation. In case the user did not specify an id field in the query, MongoDB automatically generates one. The number of documents inserted is indicated by the &#8220;nInserted&#8221; column. To observe the inserted documents, execute the provided query.<\/p>\n<pre class=\"post-pre\"><code>\r\n&gt; db.car.find()\r\n{ \"_id\" : 1, \"name\" : \"Audi\", \"color\" : \"Red\", \"cno\" : \"H101\", \"mfdcountry\" : \"Germany\", \"speed\" : 75 }\r\n{ \"_id\" : 2, \"name\" : \"Swift\", \"color\" : \"Black\", \"cno\" : \"H102\", \"mfdcountry\" : \"Italy\", \"speed\" : 60 }\r\n{ \"_id\" : 3, \"name\" : \"Maruthi800\", \"color\" : \"Blue\", \"cno\" : \"H103\", \"mfdcountry\" : \"India\", \"speed\" : 70 }\r\n{ \"_id\" : 4, \"name\" : \"Polo\", \"color\" : \"White\", \"cno\" : \"H104\", \"mfdcountry\" : \"Japan\", \"speed\" : 65 }\r\n{ \"_id\" : 5, \"name\" : \"Volkswagen\", \"color\" : \"JetBlue\", \"cno\" : \"H105\", \"mfdcountry\" : \"Rome\", \"speed\" : 80 }\r\n&gt; \r\n<\/code><\/pre>\n<p>You can learn further about the find and insert operations in MongoDB. When inserting, it is not necessary for users to include all the fields in the query. Now, let&#8217;s see how the insert process functions when certain fields are not specified.<\/p>\n<h3>Inserting multiple documents into MongoDB can be done using the bulk insert feature, where you can specify specific fields for the documents being inserted.<\/h3>\n<pre class=\"post-pre\"><code>\r\n&gt; db.car.insert(\r\n... [\r\n... { _id:6,name:\"HondaCity\",color:\"Grey\",cno:\"H106\",mfdcountry:\"Sweden\",speed:45 },\r\n... {name:\"Santro\",color:\"Pale Blue\",cno:\"H107\",mfdcountry:\"Denmark\",speed:55 },\r\n... { _id:8,name:\"Zen\",speed:54 }\r\n... ]\r\n... )\r\nBulkWriteResult({\r\n\t\"writeErrors\" : [ ],\r\n\t\"writeConcernErrors\" : [ ],\r\n\t\"nInserted\" : 3,\r\n\t\"nUpserted\" : 0,\r\n\t\"nMatched\" : 0,\r\n\t\"nModified\" : 0,\r\n\t\"nRemoved\" : 0,\r\n\t\"upserted\" : [ ]\r\n})\r\n&gt; \r\n<\/code><\/pre>\n<p>In this particular case, the user did not specify the id field for the second document and only provided the id, name, and speed fields for the third document in the query. However, despite missing some fields in both the second and third documents, the insertion query succeeded. The nInserted column confirms that three documents were inserted. You can now use the find method to verify the inserted documents.<\/p>\n<pre class=\"post-pre\"><code>\r\n&gt; db.car.find()\r\n{ \"_id\" : 1, \"name\" : \"Audi\", \"color\" : \"Red\", \"cno\" : \"H101\", \"mfdcountry\" : \"Germany\", \"speed\" : 75 }\r\n{ \"_id\" : 2, \"name\" : \"Swift\", \"color\" : \"Black\", \"cno\" : \"H102\", \"mfdcountry\" : \"Italy\", \"speed\" : 60 }\r\n{ \"_id\" : 3, \"name\" : \"Maruthi800\", \"color\" : \"Blue\", \"cno\" : \"H103\", \"mfdcountry\" : \"India\", \"speed\" : 70 }\r\n{ \"_id\" : 4, \"name\" : \"Polo\", \"color\" : \"White\", \"cno\" : \"H104\", \"mfdcountry\" : \"Japan\", \"speed\" : 65 }\r\n{ \"_id\" : 5, \"name\" : \"Volkswagen\", \"color\" : \"JetBlue\", \"cno\" : \"H105\", \"mfdcountry\" : \"Rome\", \"speed\" : 80 }\r\n{ \"_id\" : 6, \"name\" : \"HondaCity\", \"color\" : \"Grey\", \"cno\" : \"H106\", \"mfdcountry\" : \"Sweden\", \"speed\" : 45 }\r\n{ \"_id\" : ObjectId(\"54885b8e61307aec89441a0b\"), \"name\" : \"Santro\", \"color\" : \"Pale Blue\", \"cno\" : \"H107\", \"mfdcountry\" : \"Denmark\", \"speed\" : 55 }\r\n{ \"_id\" : 8, \"name\" : \"Zen\", \"speed\" : 54 }\r\n&gt; \r\n<\/code><\/pre>\n<p>Please take note that MongoDB automatically generates the id for the car &#8220;Santro&#8221;. In the case of id 8, only the name and speed fields are included during insertion.<\/p>\n<h3>Adding random documents<\/h3>\n<p>If an error occurs during the process of unordered insertion, MongoDB will still continue inserting the remaining documents in the array. To illustrate,<\/p>\n<pre class=\"post-pre\"><code>\r\n&gt; db.car.insert(\r\n... [\r\n... { _id:9,name:\"SwiftDezire\",color:\"Maroon\",cno:\"H108\",mfdcountry:\"New York\",speed:40 },\r\n... { name:\"Punto\",color:\"Wine Red\",cno:\"H109\",mfdcountry:\"Paris\",speed:45 },\r\n...  ],\r\n... { ordered: false }\r\n... )\r\nBulkWriteResult({\r\n\t\"writeErrors\" : [ ],\r\n\t\"writeConcernErrors\" : [ ],\r\n\t\"nInserted\" : 2,\r\n\t\"nUpserted\" : 0,\r\n\t\"nMatched\" : 0,\r\n\t\"nModified\" : 0,\r\n\t\"nRemoved\" : 0,\r\n\t\"upserted\" : [ ]\r\n})\r\n&gt; \r\n<\/code><\/pre>\n<p>Please run the command db.car.find() to retrieve the data from the unordered collection specified in the insert query.<\/p>\n<pre class=\"post-pre\"><code>\r\n{ \"_id\" : 1, \"name\" : \"Audi\", \"color\" : \"Red\", \"cno\" : \"H101\", \"mfdcountry\" : \"Germany\", \"speed\" : 75 }\r\n{ \"_id\" : 2, \"name\" : \"Swift\", \"color\" : \"Black\", \"cno\" : \"H102\", \"mfdcountry\" : \"Italy\", \"speed\" : 60 }\r\n{ \"_id\" : 3, \"name\" : \"Maruthi800\", \"color\" : \"Blue\", \"cno\" : \"H103\", \"mfdcountry\" : \"India\", \"speed\" : 70 }\r\n{ \"_id\" : 4, \"name\" : \"Polo\", \"color\" : \"White\", \"cno\" : \"H104\", \"mfdcountry\" : \"Japan\", \"speed\" : 65 }\r\n{ \"_id\" : 5, \"name\" : \"Volkswagen\", \"color\" : \"JetBlue\", \"cno\" : \"H105\", \"mfdcountry\" : \"Rome\", \"speed\" : 80 }\r\n{ \"_id\" : 6, \"name\" : \"HondaCity\", \"color\" : \"Grey\", \"cno\" : \"H106\", \"mfdcountry\" : \"Sweden\", \"speed\" : 45 }\r\n{ \"_id\" : ObjectId(\"54746407d785e3a05a1808a6\"), \"name\" : \"Santro\", \"color\" : \"Pale Blue\", \"cno\" : \"H107\", \"mfdcountry\" : \"Denmark\", \"speed\" : 55 }\r\n{ \"_id\" : 8, \"name\" : \"Zen\", \"speed\" : 54 }\r\n{ \"_id\" : 9, \"name\" : \"SwiftDezire\", \"color\" : \"Maroon\", \"cno\" : \"H108\", \"mfdcountry\" : \"New York\", \"speed\" : 40 }\r\n{ \"_id\" : ObjectId(\"5474642dd785e3a05a1808a7\"), \"name\" : \"Punto\", \"color\" : \"Wine Red\", \"cno\" : \"H109\", \"mfdcountry\" : \"Paris\", \"speed\" : 45 }\r\n<\/code><\/pre>\n<p>Once the documents are added, you will notice that it is an unordered insertion. In case the insert method experiences an issue, the outcome will include the &#8220;WriteResult.writeErrors&#8221; field which displays the error message responsible for the failure.<\/p>\n<h3>Adding identical identification value.<\/h3>\n<pre class=\"post-pre\"><code>\r\n&gt; db.car.insert({_id:6,name:\"Innova\"})\r\nWriteResult({\r\n\t\"nInserted\" : 0,\r\n\t\"writeError\" : {\r\n\t\t\"code\" : 11000,\r\n\t\t\"errmsg\" : \"insertDocument :: caused by :: 11000 E11000 duplicate key error index: scdev.car.$_id_  dup key: { : 6.0 }\"\r\n\t}\r\n})\r\n&gt; \r\n<\/code><\/pre>\n<p>The error message suggests that we are attempting to insert a document with the same ID (6) as an existing document, resulting in a duplicate key error.<\/p>\n<h3>The method Bulk.insert() in MongoDB is capable of inserting multiple documents at once.<\/h3>\n<p>From version 2.6 onwards, this technique allows for the insertion of multiple documents at once. It is achieved using the Bulk.insert() syntax, where the parameter refers to the specific document to be inserted. Let&#8217;s now observe an instance illustrating bulk insertion.<\/p>\n<h3>Adding a large number of unordered items simultaneously.<\/h3>\n<pre class=\"post-pre\"><code>\r\n&gt; var carbulk = db.car.initializeUnorderedBulkOp();\r\n&gt; carbulk.insert({ name:\"Ritz\", color:\"Grey\",cno:\"H109\",mfdcountry:\"Mexico\",speed:62});\r\n&gt; carbulk.insert({ name:\"Versa\", color:\"Magenta\",cno:\"H110\",mfdcountry:\"France\",speed:68});\r\n&gt; carbulk.insert({ name:\"Innova\", color:\"JetRed\",cno:\"H111\",mfdcountry:\"Dubai\",speed:72});\r\n&gt; carbulk.execute();\r\nBulkWriteResult({\r\n\t\"writeErrors\" : [ ],\r\n\t\"writeConcernErrors\" : [ ],\r\n\t\"nInserted\" : 3,\r\n\t\"nUpserted\" : 0,\r\n\t\"nMatched\" : 0,\r\n\t\"nModified\" : 0,\r\n\t\"nRemoved\" : 0,\r\n\t\"upserted\" : [ ]\r\n})\r\n&gt; \r\n<\/code><\/pre>\n<p>A carbulk unordered list is created and an insert query is defined with the fields and values that need to be inserted. It is important to call the execute() method after the last insert statement to ensure that the data is successfully inserted into the database.<\/p>\n<h3>Bulk ordered insertion in MongoDB<\/h3>\n<p>We employ the initializeOrderedBulkOp method to achieve a concept similar to unordered bulk insert.<\/p>\n<pre class=\"post-pre\"><code>\r\n&gt;var car1bulk = db.car.initializeOrderedBulkOp();\r\n&gt;car1bulk.insert({ name:\"Ertiga\", color:\"Red\",cno:\"H112\",mfdcountry:\"America\",speed:65});\r\n&gt;car1bulk.insert({ name:\"Quanta\", color:\"Maroon\",cno:\"H113\",mfdcountry:\"Rome\",speed:78});\r\n&gt;car1bulk.execute();\r\nBulkWriteResult({\r\n\t\"writeErrors\" : [ ],\r\n\t\"writeConcernErrors\" : [ ],\r\n\t\"nInserted\" : 2,\r\n\t\"nUpserted\" : 0,\r\n\t\"nMatched\" : 0,\r\n\t\"nModified\" : 0,\r\n\t\"nRemoved\" : 0,\r\n\t\"upserted\" : [ ]\r\n})\r\n<\/code><\/pre>\n<p>We begin by generating a sorted compilation of cars called carbulk1, followed by adding the documents using the execute() function.<\/p>\n<h3>A Java program for performing bulk insertion in MongoDB.<\/h3>\n<p>Here&#8217;s a Java program that demonstrates various bulk operations, similar to those performed using shell commands. This program focuses on bulk insertion and uses the MongoDB Java driver version 2.x.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.mongodb;\r\n\r\nimport com.mongodb.BasicDBObject;\r\nimport com.mongodb.BulkWriteOperation;\r\nimport com.mongodb.BulkWriteResult;\r\nimport com.mongodb.DB;\r\nimport com.mongodb.DBCollection;\r\nimport com.mongodb.DBCursor;\r\nimport com.mongodb.DBObject;\r\nimport com.mongodb.MongoClient;\r\nimport java.net.UnknownHostException;\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\npublic class MongoDBBulkInsert {\r\n\r\n\t\/\/method that inserts all the documents \r\n    public static void insertmultipledocs() throws UnknownHostException{\r\n    \r\n    \/\/Get a new connection to the db assuming that it is running    \r\n \r\n     MongoClient mongoClient = new MongoClient(\"localhost\");\r\n    \r\n     \/\/\/\/use test as a datbase,use your database here \r\n     DB db=mongoClient.getDB(\"test\");\r\n     \r\n     \/\/\/\/fetch the collection object ,car is used here,use your own \r\n     DBCollection coll = db.getCollection(\"car\");\r\n     \r\n    \/\/create a new object\r\n    DBObject d1 = new BasicDBObject();\r\n    \r\n    \/\/data for object d1\r\n    d1.put(\"_id\", 11);\r\n    d1.put(\"name\",\"WagonR\");\r\n    d1.put(\"color\", \"MetallicSilver\");\r\n    d1.put(\"cno\", \"H141\");\r\n    d1.put(\"mfdcountry\",\"Australia\");\r\n    d1.put(\"speed\",66);\r\n    \r\n    DBObject d2 = new BasicDBObject();\r\n    \r\n    \/\/data for object d2\r\n    d2.put(\"_id\", 12);\r\n    d2.put(\"name\",\"Xylo\");\r\n    d2.put(\"color\", \"JetBlue\");\r\n    d2.put(\"cno\", \"H142\");\r\n    d2.put(\"mfdcountry\",\"Europe\");\r\n    d2.put(\"speed\",69);\r\n        \r\n    \r\n    DBObject d3 = new BasicDBObject();\r\n    \r\n    \/\/data for object d3\r\n    d3.put(\"_id\", 13);\r\n    d3.put(\"name\",\"Alto800\");\r\n    d3.put(\"color\", \"JetGrey\");\r\n    d3.put(\"cno\", \"H143\");\r\n    d3.put(\"mfdcountry\",\"Austria\");\r\n    d3.put(\"speed\",74);\r\n    \r\n    \/\/create a new list\r\n    List&lt;DBObject&gt; docs = new ArrayList&lt;&gt;();\r\n    \r\n    \/\/add d1,d2 and d3 to list docs\r\n    docs.add(d1);\r\n    docs.add(d2);\r\n    docs.add(d3);\r\n    \r\n    \/\/insert list docs to collection\r\n    coll.insert(docs);\r\n    \r\n    \r\n    \/\/stores the result in cursor\r\n    DBCursor carmuldocs = coll.find();\r\n    \r\n    \r\n    \/\/print the contents of the cursor\r\n     try {\r\n         while(carmuldocs.hasNext()) {\r\n       System.out.println(carmuldocs.next());\r\n        }\r\n    }        finally {\r\n            carmuldocs.close();\/\/close the cursor\r\n    } \r\n    \r\n    \r\n    }\r\n    \r\n    \/\/method that inserts documents with some fields\r\n    public static void insertsomefieldsformultipledocs() throws UnknownHostException{\r\n    \r\n    \/\/Get a new connection to the db assuming that it is running    \r\n \r\n     MongoClient mongoClient = new MongoClient(\"localhost\");\r\n    \r\n     \/\/\/\/use test as a datbase,use your database here \r\n     DB db=mongoClient.getDB(\"test\");\r\n     \r\n     \/\/\/\/fetch the collection object ,car is used here,use your own \r\n     DBCollection coll = db.getCollection(\"car\");\r\n    \r\n    \/\/create object d1 \r\n    DBObject d1 = new BasicDBObject();\r\n    \r\n    \/\/insert data for name,color and speed\r\n    d1.put(\"name\",\"Indica\");\r\n    d1.put(\"color\", \"Silver\");\r\n    d1.put(\"cno\", \"H154\");\r\n    \r\n    \r\n    DBObject d2 = new BasicDBObject();\r\n    \r\n    \/\/insert data for id,name and speed\r\n    d2.put(\"_id\", 43);\r\n    d2.put(\"name\",\"Astar\");\r\n    \r\n    d2.put(\"speed\",79);\r\n        \r\n    \r\n    \r\n    \r\n    List&lt;DBObject&gt; docs = new ArrayList&lt;&gt;();\r\n    docs.add(d1);\r\n    docs.add(d2);\r\n   \r\n    \r\n    coll.insert(docs);\r\n    \r\n    DBCursor carmuldocs = coll.find();\r\n    \r\n     System.out.println(\"-----------------------------------------------\");\r\n     try {\r\n         while(carmuldocs.hasNext()) {\r\n       System.out.println(carmuldocs.next());\r\n        }\r\n    }        finally {\r\n            carmuldocs.close();\/\/close the cursor\r\n    } \r\n    \r\n    \r\n    }\r\n    \r\n    \/\/method that checks for duplicate documents\r\n    public static void insertduplicatedocs() throws UnknownHostException{\r\n    \r\n    \/\/Get a new connection to the db assuming that it is running    \r\n \r\n     MongoClient mongoClient = new MongoClient(\"localhost\");\r\n    \r\n     \/\/\/\/use test as a datbase,use your database here \r\n     DB db=mongoClient.getDB(\"test\");\r\n     \r\n     \/\/\/\/fetch the collection object ,car is used here,use your own \r\n     DBCollection coll = db.getCollection(\"car\");\r\n     \r\n    DBObject d1 = new BasicDBObject();\r\n    \r\n    \/\/insert duplicate data of id11\r\n    d1.put(\"_id\", 11);\r\n    d1.put(\"name\",\"WagonR-Lxi\");\r\n    \r\n    coll.insert(d1);\r\n    \r\n   \r\n    DBCursor carmuldocs = coll.find();\r\n    \r\n     System.out.println(\"-----------------------------------------------\");\r\n     try {\r\n         while(carmuldocs.hasNext()) {\r\n       System.out.println(carmuldocs.next());\r\n        }\r\n    }        finally {\r\n            carmuldocs.close();\/\/close the cursor\r\n    } \r\n    \r\n    \r\n    }\r\n    \r\n    \/\/method to perform bulk unordered list\r\n    public static void insertbulkunordereddocs() throws UnknownHostException{\r\n    \r\n    \/\/Get a new connection to the db assuming that it is running    \r\n \r\n     MongoClient mongoClient = new MongoClient(\"localhost\");\r\n    \r\n     \/\/\/\/use test as a datbase,use your database here \r\n     DB db=mongoClient.getDB(\"test\");\r\n     \r\n     \/\/\/\/fetch the collection object ,car is used here,use your own \r\n     DBCollection coll = db.getCollection(\"car\");\r\n     \r\n    DBObject d1 = new BasicDBObject();\r\n    \r\n    \r\n    d1.put(\"name\",\"Suzuki S-4\");\r\n    d1.put(\"color\", \"Yellow\");\r\n    d1.put(\"cno\", \"H167\");\r\n    d1.put(\"mfdcountry\",\"Italy\");\r\n    d1.put(\"speed\",54);\r\n    \r\n    DBObject d2 = new BasicDBObject();\r\n    \r\n    \r\n    d2.put(\"name\",\"Santro-Xing\");\r\n    d2.put(\"color\", \"Cyan\");\r\n    d2.put(\"cno\", \"H164\");\r\n    d2.put(\"mfdcountry\",\"Holand\");\r\n    d2.put(\"speed\",76);\r\n        \r\n    \/\/intialize and create a unordered bulk\r\n    BulkWriteOperation  b1 = coll.initializeUnorderedBulkOperation();\r\n    \r\n    \/\/insert d1 and d2 to bulk b1\r\n    b1.insert(d1);\r\n    b1.insert(d2);\r\n    \r\n    \/\/execute the bulk\r\n    BulkWriteResult  r1 = b1.execute();\r\n    \r\n    \r\n    \r\n    DBCursor carmuldocs = coll.find();\r\n    \r\n    System.out.println(\"-----------------------------------------------\");\r\n     try {\r\n         while(carmuldocs.hasNext()) {\r\n       System.out.println(carmuldocs.next());\r\n        }\r\n    }        finally {\r\n            carmuldocs.close();\/\/close the cursor\r\n    } \r\n    \r\n    \r\n    }\r\n    \r\n    \/\/method that performs bulk insert for ordered list\r\n       public static void insertbulkordereddocs() throws UnknownHostException{\r\n    \r\n    \/\/Get a new connection to the db assuming that it is running    \r\n \r\n     MongoClient mongoClient = new MongoClient(\"localhost\");\r\n    \r\n     \/\/\/\/use test as a datbase,use your database here \r\n     DB db=mongoClient.getDB(\"test\");\r\n     \r\n     \/\/\/\/fetch the collection object ,car is used here,use your own \r\n     DBCollection coll = db.getCollection(\"car\");\r\n     \r\n    DBObject d1 = new BasicDBObject();\r\n    \r\n    \r\n    d1.put(\"name\",\"Palio\");\r\n    d1.put(\"color\", \"Purple\");\r\n    d1.put(\"cno\", \"H183\");\r\n    d1.put(\"mfdcountry\",\"Venice\");\r\n    d1.put(\"speed\",82);\r\n    \r\n    DBObject d2 = new BasicDBObject();\r\n    \r\n    \r\n    d2.put(\"name\",\"Micra\");\r\n    d2.put(\"color\", \"Lime\");\r\n    d2.put(\"cno\", \"H186\");\r\n    d2.put(\"mfdcountry\",\"Ethopia\");\r\n    d2.put(\"speed\",84);\r\n        \r\n    \/\/initialize and create ordered bulk \r\n    BulkWriteOperation  b1 = coll.initializeOrderedBulkOperation();\r\n    \r\n    b1.insert(d1);\r\n    b1.insert(d2);\r\n    \r\n    \/\/invoking execute\r\n    BulkWriteResult  r1 = b1.execute();\r\n    \r\n    \r\n    \r\n    DBCursor carmuldocs = coll.find();\r\n    \r\n    System.out.println(\"-----------------------------------\");\r\n    \r\n     try {\r\n         while(carmuldocs.hasNext()) {\r\n       System.out.println(carmuldocs.next());\r\n        }\r\n    }        finally {\r\n            carmuldocs.close();\/\/close the cursor\r\n    } \r\n    \r\n    \r\n    }\r\n    \r\n    \r\n    public static void main(String[] args) throws UnknownHostException{\r\n        \r\n       \/\/invoke all the methods to perform insert operation\r\n       \r\n       insertmultipledocs();\r\n       insertsomefieldsformultipledocs();\r\n       \r\n        insertbulkunordereddocs();\r\n        insertbulkordereddocs();\r\n        insertduplicatedocs();\r\n    }\r\n\r\n}\r\n<\/code><\/pre>\n<p>The program above has generated the following output.<\/p>\n<pre class=\"post-pre\"><code>\r\n{ \"_id\" : 11 , \"name\" : \"WagonR\" , \"color\" : \"MetallicSilver\" , \"cno\" : \"H141\" , \"mfdcountry\" : \"Australia\" , \"speed\" : 66}\r\n{ \"_id\" : 12 , \"name\" : \"Xylo\" , \"color\" : \"JetBlue\" , \"cno\" : \"H142\" , \"mfdcountry\" : \"Europe\" , \"speed\" : 69}\r\n{ \"_id\" : 13 , \"name\" : \"Alto800\" , \"color\" : \"JetGrey\" , \"cno\" : \"H143\" , \"mfdcountry\" : \"Austria\" , \"speed\" : 74}\r\n-----------------------------------------------\r\n{ \"_id\" : 11 , \"name\" : \"WagonR\" , \"color\" : \"MetallicSilver\" , \"cno\" : \"H141\" , \"mfdcountry\" : \"Australia\" , \"speed\" : 66}\r\n{ \"_id\" : 12 , \"name\" : \"Xylo\" , \"color\" : \"JetBlue\" , \"cno\" : \"H142\" , \"mfdcountry\" : \"Europe\" , \"speed\" : 69}\r\n{ \"_id\" : 13 , \"name\" : \"Alto800\" , \"color\" : \"JetGrey\" , \"cno\" : \"H143\" , \"mfdcountry\" : \"Austria\" , \"speed\" : 74}\r\n{ \"_id\" : { \"$oid\" : \"548860e803649b8efac5a1d7\"} , \"name\" : \"Indica\" , \"color\" : \"Silver\" , \"cno\" : \"H154\"}\r\n{ \"_id\" : 43 , \"name\" : \"Astar\" , \"speed\" : 79}\r\n-----------------------------------------------\r\n{ \"_id\" : 11 , \"name\" : \"WagonR\" , \"color\" : \"MetallicSilver\" , \"cno\" : \"H141\" , \"mfdcountry\" : \"Australia\" , \"speed\" : 66}\r\n{ \"_id\" : 12 , \"name\" : \"Xylo\" , \"color\" : \"JetBlue\" , \"cno\" : \"H142\" , \"mfdcountry\" : \"Europe\" , \"speed\" : 69}\r\n{ \"_id\" : 13 , \"name\" : \"Alto800\" , \"color\" : \"JetGrey\" , \"cno\" : \"H143\" , \"mfdcountry\" : \"Austria\" , \"speed\" : 74}\r\n{ \"_id\" : { \"$oid\" : \"548860e803649b8efac5a1d7\"} , \"name\" : \"Indica\" , \"color\" : \"Silver\" , \"cno\" : \"H154\"}\r\n{ \"_id\" : 43 , \"name\" : \"Astar\" , \"speed\" : 79}\r\n{ \"_id\" : { \"$oid\" : \"548860e803649b8efac5a1d8\"} , \"name\" : \"Suzuki S-4\" , \"color\" : \"Yellow\" , \"cno\" : \"H167\" , \"mfdcountry\" : \"Italy\" , \"speed\" : 54}\r\n{ \"_id\" : { \"$oid\" : \"548860e803649b8efac5a1d9\"} , \"name\" : \"Santro-Xing\" , \"color\" : \"Cyan\" , \"cno\" : \"H164\" , \"mfdcountry\" : \"Holand\" , \"speed\" : 76}\r\n-----------------------------------\r\n{ \"_id\" : 11 , \"name\" : \"WagonR\" , \"color\" : \"MetallicSilver\" , \"cno\" : \"H141\" , \"mfdcountry\" : \"Australia\" , \"speed\" : 66}\r\n{ \"_id\" : 12 , \"name\" : \"Xylo\" , \"color\" : \"JetBlue\" , \"cno\" : \"H142\" , \"mfdcountry\" : \"Europe\" , \"speed\" : 69}\r\n{ \"_id\" : 13 , \"name\" : \"Alto800\" , \"color\" : \"JetGrey\" , \"cno\" : \"H143\" , \"mfdcountry\" : \"Austria\" , \"speed\" : 74}\r\n{ \"_id\" : { \"$oid\" : \"548860e803649b8efac5a1d7\"} , \"name\" : \"Indica\" , \"color\" : \"Silver\" , \"cno\" : \"H154\"}\r\n{ \"_id\" : 43 , \"name\" : \"Astar\" , \"speed\" : 79}\r\n{ \"_id\" : { \"$oid\" : \"548860e803649b8efac5a1d8\"} , \"name\" : \"Suzuki S-4\" , \"color\" : \"Yellow\" , \"cno\" : \"H167\" , \"mfdcountry\" : \"Italy\" , \"speed\" : 54}\r\n{ \"_id\" : { \"$oid\" : \"548860e803649b8efac5a1d9\"} , \"name\" : \"Santro-Xing\" , \"color\" : \"Cyan\" , \"cno\" : \"H164\" , \"mfdcountry\" : \"Holand\" , \"speed\" : 76}\r\n{ \"_id\" : { \"$oid\" : \"548860e803649b8efac5a1da\"} , \"name\" : \"Palio\" , \"color\" : \"Purple\" , \"cno\" : \"H183\" , \"mfdcountry\" : \"Venice\" , \"speed\" : 82}\r\n{ \"_id\" : { \"$oid\" : \"548860e803649b8efac5a1db\"} , \"name\" : \"Micra\" , \"color\" : \"Lime\" , \"cno\" : \"H186\" , \"mfdcountry\" : \"Ethopia\" , \"speed\" : 84}\r\nException in thread \"main\" com.mongodb.MongoException$DuplicateKey: { \"serverUsed\" : \"localhost:27017\" , \"ok\" : 1 , \"n\" : 0 , \"err\" : \"insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.car.$_id_  dup key: { : 11 }\" , \"code\" : 11000}\r\n\tat com.mongodb.CommandResult.getWriteException(CommandResult.java:88)\r\n\tat com.mongodb.CommandResult.getException(CommandResult.java:79)\r\n\tat com.mongodb.DBCollectionImpl.translateBulkWriteException(DBCollectionImpl.java:314)\r\n\tat com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:189)\r\n\tat com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:165)\r\n\tat com.mongodb.DBCollection.insert(DBCollection.java:93)\r\n\tat com.mongodb.DBCollection.insert(DBCollection.java:78)\r\n\tat com.mongodb.DBCollection.insert(DBCollection.java:120)\r\n\tat com.scdev.mongodb.MongoDBBulkInsert.insertduplicatedocs(MongoDBBulkInsert.java:163)\r\n\tat com.scdev.mongodb.MongoDBBulkInsert.main(MongoDBBulkInsert.java:304)\r\n<\/code><\/pre>\n<p>If you&#8217;re utilizing the MongoDB java driver version 3.x, then employ the program provided below.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.mongodb.main;\r\n\r\nimport java.net.UnknownHostException;\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\nimport org.bson.Document;\r\n\r\nimport com.mongodb.MongoClient;\r\nimport com.mongodb.client.FindIterable;\r\nimport com.mongodb.client.MongoCollection;\r\nimport com.mongodb.client.MongoDatabase;\r\n\r\npublic class MongoDBBulkInsert {\r\n\r\n\tpublic static void main(String[] args) throws UnknownHostException {\r\n\r\n\t\t\/\/ invoke all the methods to perform insert operation\r\n\r\n\t\tinsertmultipledocs();\r\n\t\tinsertsomefieldsformultipledocs();\r\n\r\n\t\tinsertduplicatedocs();\r\n\t}\r\n\r\n\t\/\/ method that inserts all the documents\r\n\tpublic static void insertmultipledocs() throws UnknownHostException {\r\n\r\n\t\t\/\/ Get a new connection to the db assuming that it is running\r\n\r\n\t\tMongoClient mongoClient = new MongoClient(\"localhost\");\r\n\r\n\t\t\/\/\/\/ use test as a database,use your database here\r\n\t\tMongoDatabase db = mongoClient.getDatabase(\"test\");\r\n\r\n\t\t\/\/\/\/ fetch the collection object ,car is used here,use your own\r\n\t\tMongoCollection&lt;Document&gt; coll = db.getCollection(\"car\");\r\n\r\n\t\t\/\/ create a new object\r\n\t\tDocument d1 = new Document();\r\n\r\n\t\t\/\/ data for object d1\r\n\t\td1.put(\"_id\", 11);\r\n\t\td1.put(\"name\", \"WagonR\");\r\n\t\td1.put(\"color\", \"MetallicSilver\");\r\n\t\td1.put(\"cno\", \"H141\");\r\n\t\td1.put(\"mfdcountry\", \"Australia\");\r\n\t\td1.put(\"speed\", 66);\r\n\r\n\t\tDocument d2 = new Document();\r\n\r\n\t\t\/\/ data for object d2\r\n\t\td2.put(\"_id\", 12);\r\n\t\td2.put(\"name\", \"Xylo\");\r\n\t\td2.put(\"color\", \"JetBlue\");\r\n\t\td2.put(\"cno\", \"H142\");\r\n\t\td2.put(\"mfdcountry\", \"Europe\");\r\n\t\td2.put(\"speed\", 69);\r\n\r\n\t\tDocument d3 = new Document();\r\n\r\n\t\t\/\/ data for object d3\r\n\t\td3.put(\"_id\", 13);\r\n\t\td3.put(\"name\", \"Alto800\");\r\n\t\td3.put(\"color\", \"JetGrey\");\r\n\t\td3.put(\"cno\", \"H143\");\r\n\t\td3.put(\"mfdcountry\", \"Austria\");\r\n\t\td3.put(\"speed\", 74);\r\n\r\n\t\t\/\/ create a new list\r\n\t\tList&lt;Document&gt; docs = new ArrayList&lt;&gt;();\r\n\r\n\t\t\/\/ add d1,d2 and d3 to list docs\r\n\t\tdocs.add(d1);\r\n\t\tdocs.add(d2);\r\n\t\tdocs.add(d3);\r\n\r\n\t\t\/\/ insert list docs to collection\r\n\t\tcoll.insertMany(docs);\r\n\r\n\t\t\/\/ stores the result in cursor\r\n\t\tFindIterable&lt;Document&gt; carmuldocs = coll.find();\r\n\r\n\t\tfor (Document d : carmuldocs)\r\n\t\t\tSystem.out.println(d);\r\n\r\n\t\tmongoClient.close();\r\n\t}\r\n\r\n\t\/\/ method that inserts documents with some fields\r\n\tpublic static void insertsomefieldsformultipledocs() throws UnknownHostException {\r\n\r\n\t\t\/\/ Get a new connection to the db assuming that it is running\r\n\r\n\t\tMongoClient mongoClient = new MongoClient(\"localhost\");\r\n\r\n\t\t\/\/\/\/ use test as a datbase,use your database here\r\n\t\tMongoDatabase db = mongoClient.getDatabase(\"test\");\r\n\r\n\t\t\/\/\/\/ fetch the collection object ,car is used here,use your own\r\n\t\tMongoCollection&lt;Document&gt; coll = db.getCollection(\"car\");\r\n\r\n\t\t\/\/ create object d1\r\n\t\tDocument d1 = new Document();\r\n\r\n\t\t\/\/ insert data for name,color and speed\r\n\t\td1.put(\"name\", \"Indica\");\r\n\t\td1.put(\"color\", \"Silver\");\r\n\t\td1.put(\"cno\", \"H154\");\r\n\r\n\t\tDocument d2 = new Document();\r\n\r\n\t\t\/\/ insert data for id,name and speed\r\n\t\td2.put(\"_id\", 43);\r\n\t\td2.put(\"name\", \"Astar\");\r\n\r\n\t\td2.put(\"speed\", 79);\r\n\r\n\t\tList&lt;Document&gt; docs = new ArrayList&lt;&gt;();\r\n\t\tdocs.add(d1);\r\n\t\tdocs.add(d2);\r\n\r\n\t\tcoll.insertMany(docs);\r\n\r\n\t\tFindIterable&lt;Document&gt; carmuldocs = coll.find();\r\n\r\n\t\tSystem.out.println(\"-----------------------------------------------\");\r\n\r\n\t\tfor (Document d : carmuldocs)\r\n\t\t\tSystem.out.println(d);\r\n\r\n\t\tmongoClient.close();\r\n\r\n\t}\r\n\r\n\t\/\/ method that checks for duplicate documents\r\n\tpublic static void insertduplicatedocs() throws UnknownHostException {\r\n\r\n\t\t\/\/ Get a new connection to the db assuming that it is running\r\n\r\n\t\tMongoClient mongoClient = new MongoClient(\"localhost\");\r\n\r\n\t\t\/\/\/\/ use test as a database, use your database here\r\n\t\tMongoDatabase db = mongoClient.getDatabase(\"test\");\r\n\r\n\t\t\/\/\/\/ fetch the collection object ,car is used here,use your own\r\n\t\tMongoCollection&lt;Document&gt; coll = db.getCollection(\"car\");\r\n\r\n\t\tDocument d1 = new Document();\r\n\r\n\t\t\/\/ insert duplicate data of id11\r\n\t\td1.put(\"_id\", 11);\r\n\t\td1.put(\"name\", \"WagonR-Lxi\");\r\n\r\n\t\tcoll.insertOne(d1);\r\n\r\n\t\tFindIterable&lt;Document&gt; carmuldocs = coll.find();\r\n\r\n\t\tSystem.out.println(\"-----------------------------------------------\");\r\n\r\n\t\tfor (Document d : carmuldocs)\r\n\t\t\tSystem.out.println(d);\r\n\r\n\t\tmongoClient.close();\r\n\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<div><img decoding=\"async\" class=\"post-images\" title=\"\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655ce554c40ba52feef2a22f\/38-0.png\" alt=\"MongoDB bulk insert, mongodb insertMany\" \/><\/div>\n<div><\/div>\n<div>More other tutorials<\/div>\n<div><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/jquery-tree-traversal-functions\/\" target=\"_blank\" rel=\"noopener\">jQuery parent() and children() tree traversal functions<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/div>\n<div><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/tutorial-on-hibernate-tomcat-jndi-datasource\/\" target=\"_blank\" rel=\"noopener\">Tutorial on how to set up a Hibernate Tomcat JNDI DataSource.<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today, we will explore the concept of MongoDB bulk insert. In MongoDB, it is possible to insert multiple documents simultaneously using the bulk insert operation. This operation involves passing an array of documents as a parameter to the insert method. One possibility for rephrasing the statement could be: &#8220;Implementing batch insert in MongoDB.&#8221; By default, [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-792","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>insertMany function for bulk insertion into a MongoDB database. - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"insertMany functions we will explore the concept of MongoDB bulk insert . In MongoDB, it is possible to insert multiple documents\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"insertMany function for bulk insertion into a MongoDB database.\" \/>\n<meta property=\"og:description\" content=\"insertMany functions we will explore the concept of MongoDB bulk insert . In MongoDB, it is possible to insert multiple documents\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-28T22:57:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-05T14:22:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655ce554c40ba52feef2a22f\/38-0.png\" \/>\n<meta name=\"author\" content=\"William Carter\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"William Carter\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/\"},\"author\":{\"name\":\"William Carter\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0\"},\"headline\":\"insertMany function for bulk insertion into a MongoDB database.\",\"datePublished\":\"2023-03-28T22:57:34+00:00\",\"dateModified\":\"2024-03-05T14:22:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/\"},\"wordCount\":685,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/\",\"name\":\"insertMany function for bulk insertion into a MongoDB database. - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2023-03-28T22:57:34+00:00\",\"dateModified\":\"2024-03-05T14:22:51+00:00\",\"description\":\"insertMany functions we will explore the concept of MongoDB bulk insert . In MongoDB, it is possible to insert multiple documents\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"insertMany function for bulk insertion into a MongoDB database.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0\",\"name\":\"William Carter\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g\",\"caption\":\"William Carter\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/williamcarter\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"insertMany function for bulk insertion into a MongoDB database. - Blog - Silicon Cloud","description":"insertMany functions we will explore the concept of MongoDB bulk insert . In MongoDB, it is possible to insert multiple documents","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/","og_locale":"en_US","og_type":"article","og_title":"insertMany function for bulk insertion into a MongoDB database.","og_description":"insertMany functions we will explore the concept of MongoDB bulk insert . In MongoDB, it is possible to insert multiple documents","og_url":"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2023-03-28T22:57:34+00:00","article_modified_time":"2024-03-05T14:22:51+00:00","og_image":[{"url":"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655ce554c40ba52feef2a22f\/38-0.png"}],"author":"William Carter","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"William Carter","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/"},"author":{"name":"William Carter","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0"},"headline":"insertMany function for bulk insertion into a MongoDB database.","datePublished":"2023-03-28T22:57:34+00:00","dateModified":"2024-03-05T14:22:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/"},"wordCount":685,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/","url":"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/","name":"insertMany function for bulk insertion into a MongoDB database. - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2023-03-28T22:57:34+00:00","dateModified":"2024-03-05T14:22:51+00:00","description":"insertMany functions we will explore the concept of MongoDB bulk insert . In MongoDB, it is possible to insert multiple documents","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/insertmany-function-for-bulk-insertion\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"insertMany function for bulk insertion into a MongoDB database."}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0","name":"William Carter","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g","caption":"William Carter"},"url":"https:\/\/www.silicloud.com\/blog\/author\/williamcarter\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=792"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/792\/revisions"}],"predecessor-version":[{"id":1658,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/792\/revisions\/1658"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}