对于大量插入,请在MongoDB中使用insertMany()的概念。insertMany()将多个文档插入一个集合中。
让我们创建一个包含文档的集合-
> db.demo729.insertMany( [ ... { BankName:"HDFC Bank",cardType:"Credit","CustomerName":[{Name:"Chris",Age:25}]}, ... { BankName:"ICICI Bank",cardType:"Debit","CustomerName":[{Name:"Bob",Age:22}]}, ... { BankName:"Kotak Bank",cardType:"Debit","CustomerName":[{Name:"David",Age:23}]} ... ] ); { "acknowledged" : true, "insertedIds" : [ ObjectId("5eac510d56e85a39df5f6333"), ObjectId("5eac510d56e85a39df5f6334"), ObjectId("5eac510d56e85a39df5f6335") ] }
在find()方法的帮助下显示集合中的所有文档-
> db.demo729.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5eac510d56e85a39df5f6333"), "BankName" : "HDFC Bank", "cardType" : "Credit", "CustomerName" : [ { "Name" : "Chris", "Age" : 25 } ] } { "_id" : ObjectId("5eac510d56e85a39df5f6334"), "BankName" : "ICICI Bank", "cardType" : "Debit", "CustomerName" : [ { "Name" : "Bob", "Age" : 22 } ] } { "_id" : ObjectId("5eac510d56e85a39df5f6335"), "BankName" : "Kotak Bank", "cardType" : "Debit", "CustomerName" : [ { "Name" : "David", "Age" : 23 } ] }