对于批量插入,可以insert()
在MongoDB中使用。让我们创建一个包含文档的集合-
> 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" : [ ] })
在find()
方法的帮助下显示集合中的所有文档-
> db.demo255.find();
这将产生以下输出-
{ "_id" : ObjectId("5e47a25e1627c0c63e7dba82"), "Name" : "Chris", "Age" : 24 } { "_id" : ObjectId("5e47a25e1627c0c63e7dba83"), "Name" : "Bob", "Age" : 22 } { "_id" : ObjectId("5e47a25e1627c0c63e7dba84"), "Name" : "David", "Age" : 23 }