mongodb 批量插入
We will look into MongoDB bulk insert today. Multiple documents can be inserted at a time in MongoDB using bulk insert operation where an array of documents is passed to the insert method as parameter.
我们今天将研究MongoDB批量插入。 可以使用批量插入操作一次将多个文档插入MongoDB中,在该操作中,文档的数组作为参数传递给insert方法。
MongoDB bulk insert performs ordered insert by default. If an error occurs during the insertion at a certain point, the insertion does not happen for the remaining documents.
MongoDB批量插入默认情况下执行有序插入。 如果在插入过程中的某个点发生错误,则其余文档不会插入。
Lets see an example of how to insert multiple documents using mongodb bulk insert through command line.
让我们看一个如何通过命令行使用mongodb批量插入来插入多个文档的示例。
> db.car.insert(
... [
... { _id:1,name:"Audi",color:"Red",cno:"H101",mfdcountry:"Germany",speed:75 },
... { _id:2,name:"Swift",color:"Black",cno:"H102",mfdcountry:"Italy",speed:60 },
... { _id:3,name:"Maruthi800",color:"Blue",cno:"H103",mfdcountry:"India",speed:70 },
... { _id:4,name:"Polo",color:"White",cno:"H104",mfdcountry:"Japan",speed:65 },
... { _id:5,name:"Volkswagen",color:"JetBlue",cno:"H105",mfdcountry:"Rome",speed:80 }
... ]
... )
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 5,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
This operation inserted five documents. MongoDB creates an id field automatically if not specified by the user in the query. The “nInserted” column tells the user number of documents that are inserted.
此操作插入了五个文档。 如果用户未在查询中指定MongoDB,则会自动创建一个id字段。 “ nInserted ”列告诉用户插入的文档数量。
To view the inserted documents perform the following query as shown below.
要查看插入的文档,请执行以下查询,如下所示。
> db.car.find()
{ "_id" : 1, "name" : "Audi", "color" : "Red", "cno" : "H101", "mfdcountry" : "Germany", "speed" : 75 }
{ "_id" : 2, "name" : "Swift", "color" : "Black", "cno" : "H102", "mfdcountry" : "Italy", "speed" : 60 }
{ "_id" : 3, "name" : "Maruthi800", "color" : "Blue", "cno" : "H103", "mfdcountry" : "India", "speed" : 70 }
{ "_id" : 4, "name" : "Polo", "color" : "White", "cno" : "H104", "mfdcountry" : "Japan", "speed" : 65 }
{ "_id" : 5, "name" : "Volkswagen", "color" : "JetBlue", "cno" : "H105", "mfdcountry" : "Rome", "speed" : 80 }
>
Read more about MongoDB find and MongoDB insert operations.
阅读有关MongoDB查找和MongoDB插入操作的更多信息。
While inserting it is not mandatory for user to provide all the fields in the query. Now lets see how the insert works when some of the fields are not specified.
插入时,用户并非必须提供查询中的所有字段。 现在让我们看看未指定某些字段时插入的工作方式。
> db.car.insert(
... [
... { _id:6,name:"HondaCity",color:"Grey",cno:"H106",mfdcountry:"Sweden",speed:45 },
... {name:"Santro",color:"Pale Blue",cno:"H107",mfdcountry:"Denmark",speed:55 },
... { _id:8,name:"Zen",speed:54 }
... ]
... )
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
>
In this example, for the second document, the id field is not specified by the user and for the third document only id, name and speed fields are supplied in the query. The query does a successful insertion even though some fields are missing in the second and third documents. The nInserted column says that three documents were inserted.
在此示例中,对于第二个文档,用户未指定id字段,而对于第三个文档,查询中仅提供了id,name和speed字段。 即使第二个和第三个文档中缺少某些字段,查询也会成功插入。 nInserted列表示已插入三个文档。
Invoke find method and check the inserted documents.
调用查找方法并检查插入的文档。
> db.car.find()
{ "_id" : 1, "name" : "Audi", "color" : "Red", "cno" : "H101", "mfdcountry" : "Germany", "speed" : 75 }
{ "_id" : 2, "name" : "Swift", "color" : "Black", "cno" : "H102", "mfdcountry" : "Italy", "speed" : 60 }
{ "_id" : 3, "name" : "Maruthi800", "color" : "Blue", "cno" : "H103", "mfdcountry" : "India", "speed" : 70 }
{ "_id" : 4, "name" : "Polo", "color" : "White", "cno" : "H104", "mfdcountry" : "Japan", "speed" : 65 }
{ "_id" : 5, "name" : "Volkswagen", "color" : "JetBlue", "cno" : "H105", "mfdcountry" : "Rome", "speed" : 80 }
{ "_id" : 6, "name" : "HondaCity", "color" : "Grey", "cno" : "H106", "mfdcountry" : "Sweden", "speed" : 45 }
{ "_id" : ObjectId("54885b8