mongodb insert()、insertOne() 和 insertMany() 方法之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36792649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between insert(), insertOne(), and insertMany() method?
提问by Marcos Mendes
What's the difference between insert()
, insertOne()
, and insertMany()
methods on MongoDB. In what situation should I use each one?
什么之间的差异insert()
,insertOne()
以及insertMany()
对MongoDB的方法。我应该在什么情况下使用每一种?
I read the docs, but it's not clear when use each one.
我阅读了文档,但不清楚何时使用每个文档。
回答by styvane
What's the difference between insert(), insertOne() and insertMany() methods on MongoDB
MongoDB 上的 insert()、insertOne() 和 insertMany() 方法有什么区别
db.collection.insert()
as mentioned in the documentation inserts a document or documents into a collection and returns a WriteResultobject for single inserts and a BulkWriteResultobject for bulk inserts.> var d = db.collection.insert({"b": 3}) > d WriteResult({ "nInserted" : 1 }) > var d2 = db.collection.insert([{"b": 3}, {'c': 4}]) > d2 BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
db.collection.insertOne()
as mentioned in the documentation inserts a document into a collection and returns a document which look like this:> var document = db.collection.insertOne({"a": 3}) > document { "acknowledged" : true, "insertedId" : ObjectId("571a218011a82a1d94c02333") }
db.collection.insertMany()
inserts multiple documents into a collection and returns a document that looks like this:> var res = db.collection.insertMany([{"b": 3}, {'c': 4}]) > res { "acknowledged" : true, "insertedIds" : [ ObjectId("571a22a911a82a1d94c02337"), ObjectId("571a22a911a82a1d94c02338") ] }
db.collection.insert()
如在文档插入一个文档或多个文档到收集提到并返回一个写结果为单个插入对象和BulkWriteResult对于批量插入对象。> var d = db.collection.insert({"b": 3}) > d WriteResult({ "nInserted" : 1 }) > var d2 = db.collection.insert([{"b": 3}, {'c': 4}]) > d2 BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
db.collection.insertOne()
如文档中所述,将文档插入集合并返回如下所示的文档:> var document = db.collection.insertOne({"a": 3}) > document { "acknowledged" : true, "insertedId" : ObjectId("571a218011a82a1d94c02333") }
db.collection.insertMany()
将多个文档插入一个集合并返回一个如下所示的文档:> var res = db.collection.insertMany([{"b": 3}, {'c': 4}]) > res { "acknowledged" : true, "insertedIds" : [ ObjectId("571a22a911a82a1d94c02337"), ObjectId("571a22a911a82a1d94c02338") ] }
In what situation should I use each one?
我应该在什么情况下使用每一种?
The insert()
method is deprecated in major driver so you should use the
the .insertOne()
method whenever you want to insert a single document into your collection and the .insertMany
when you want to insert multiple documents into your collection. Of course this is not mentioned in the documentation but the fact is that nobody really writes an application in the shell. The same thing applies to updateOne
, updateMany
, deleteOne
, deleteMany
, findOneAndDelete
, findOneAndUpdate
and findOneAndReplace
. See Write Operations Overview.
该insert()
方法在主要驱动程序中已弃用,因此.insertOne()
无论何时要将单个文档插入到集合中,以及.insertMany
在要向集合中插入多个文档时,都应使用该方法。当然,文档中没有提到这一点,但事实是没有人真正在 shell 中编写应用程序。同样的道理也适用于updateOne
,updateMany
,deleteOne
,deleteMany
,findOneAndDelete
,findOneAndUpdate
和findOneAndReplace
。请参阅写入操作概述。
回答by ayushgp
db.collection.insert()
:It allows you to insert One or more documents in the collection. Syntax:
- Single insert:
db.collection.insert({<document>});
Multiple insert:
db.collection.insert([ , , ... ]);
Returns a
WriteResult
object:WriteResult({ "nInserted" : 1 });
- Single insert:
db.collection.insertOne()
:It allows you to insert exactly 1 document in the collection. Its syntax is the same as that of single insert in
insert()
.Returns the following document:
{ "acknowledged" : true, "insertedId" : ObjectId("56fc40f9d735c28df206d078") }
db.collection.insertMany()
:It allows you to insert an array of documents in the collection. Syntax:
db.collection.insertMany( { [ <document 1> , <document 2>, ... ] });
Returns the following document:
{ "acknowledged" : true, "insertedIds" : [ ObjectId("562a94d381cb9f1cd6eb0e1a"), ObjectId("562a94d381cb9f1cd6eb0e1b"), ObjectId("562a94d381cb9f1cd6eb0e1c") ] }
db.collection.insert()
:它允许您在集合中插入一个或多个文档。句法:
- 单插:
db.collection.insert({<document>});
多次插入:
db.collection.insert([ , , ... ]);
返回一个
WriteResult
对象:WriteResult({ "nInserted" : 1 });
- 单插:
db.collection.insertOne()
:它允许您在集合中恰好插入 1 个文档。其语法与 in 中的单次插入相同
insert()
。返回以下文档:
{ "acknowledged" : true, "insertedId" : ObjectId("56fc40f9d735c28df206d078") }
db.collection.insertMany()
:它允许您在集合中插入一组文档。句法:
db.collection.insertMany( { [ <document 1> , <document 2>, ... ] });
返回以下文档:
{ "acknowledged" : true, "insertedIds" : [ ObjectId("562a94d381cb9f1cd6eb0e1a"), ObjectId("562a94d381cb9f1cd6eb0e1b"), ObjectId("562a94d381cb9f1cd6eb0e1c") ] }
All three of these also allow you to define a custom writeConcern
and also create a collection if it doesn't exist.
所有这三个还允许您定义自定义writeConcern
并创建一个集合(如果它不存在)。
回答by Andrew Nessin
There is also a difference in error handling, check here. The insert command returns a document in both success and error cases. But the insertOne and insertMany commands throwsexceptions. Exceptions are easier to handle in code, than evaluating the returned document to figure out errors. Probably the reason why they are deprecated in the drivers as mentioned in sstyvane's answer.
错误处理也有区别,请查看这里。insert 命令在成功和错误情况下都返回一个文档。但是 insertOne 和 insertMany 命令会引发异常。在代码中处理异常比评估返回的文档以找出错误更容易。可能是 sstyvane 的回答中提到的驱动程序中不推荐使用它们的原因。
回答by Leonard Vincent Luzon
If the collection does not exist, then the insertOne() method creates the collection. If you input the same data again, mongod will create another unique id to avoid duplication.
如果集合不存在,则 insertOne() 方法创建集合。如果再次输入相同的数据,mongod 将创建另一个唯一的 id 以避免重复。