On this page本页内容
db.collection.
insert
()¶mongo
Shell Method
This page documents the 本页记录了mongo
shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. mongo
shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。
Inserts a document or documents into a collection.将一个或多个文档插入到集合中。
The insert()
method has the following syntax:insert()
方法语法如下所示:
document |
document |
|
writeConcern |
document |
|
ordered |
boolean |
|
The insert()
returns an object that contains the status of the operation.insert()
返回一个包含操作状态的对象。
|
The insert()
method uses the insert
command, which uses the default write concern. insert()
方法使用insert
命令,该命令使用默认的写关注点。To specify a different write concern, include the write concern in the options parameter.要指定不同的写入关注点,请在options
参数中包含写入关注点。
If the collection does not exist, then the 如果集合不存在,则insert()
method will create the collection.insert()
方法将创建集合。
_id
If the document does not specify an _id field, then MongoDB will add the 如果文档没有指定_id
field and assign a unique ObjectId
for the document before inserting. _id
字段,则MongoDB将添加_id
字段,并在插入之前为文档分配唯一的ObjectId
。Most drivers create an ObjectId and insert the 大多数驱动程序创建_id
field, but the mongod
will create and populate the _id
if the driver or application does not.ObjectId
并插入_id
字段,但如果驱动程序或应用程序不创建并填充_id
,mongod
将创建并填充。
If the document contains an 如果文档包含_id
field, the _id
value must be unique within the collection to avoid duplicate key error._id
字段,_id
值在集合中必须是唯一的,以避免重复键错误。
db.collection.insert()
can be used inside multi-document transactions.可以在多文档事务中使用。
Important
In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. 在大多数情况下,与单文档写入相比,多文档事务会带来更大的性能成本,而多文档事务的可用性不应取代有效的模式设计。For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. 对于许多场景,非规范化数据模型(嵌入式文档和数组)将继续适合您的数据和用例。That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.也就是说,对于许多场景,适当地建模数据将最大限度地减少对多文档事务的需求。
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.有关其他事务使用注意事项(如运行时限制和oplog大小限制),请参阅生产注意事项。
Starting in MongoDB 4.4 with feature compatibility version (fcv) 从功能兼容版本(fcv)"4.4"
, you can create collections and indexes inside a multi-document transaction if the transaction is not a cross-shard write transaction."4.4"
的MongoDB 4.4开始,如果事务不是跨切分写入事务,则可以在多文档事务中创建集合和索引。
As such, for feature compatibility version (fcv) 因此,对于功能兼容性版本(fcv)"4.4"
and greater, if you specify an insert on a non-existing collection in a transaction, the collection is implicitly created."4.4"
及更高版本,如果在事务中对不存在的集合指定插入,则会隐式创建该集合。
If the feature compatibility version (fcv) is 如果功能兼容版本(fcv)为"4.2"
or less, the operation must be against an existing collection."4.2"
或更低,则操作必须针对现有集合。
Do not explicitly set the write concern for the operation if run in a transaction. 如果在事务中运行,请不要显式设置操作的写入关注点。To use write concern with transactions, see Transactions and Write Concern.要将写关注点用于事务,请参阅事务和写关注点。
The following examples insert documents into the 以下示例将文档插入products
collection. products
集合。If the collection does not exist, the 如果集合不存在,insert()
method creates the collection.insert()
方法将创建集合。
_id
Field_id
字段的情况下插入文档¶In the following example, the document passed to the 在以下示例中,传递给insert()
method does not contain the _id
field:insert()
方法的文档不包含_id
字段:
During the insert, 在插入过程中,mongod
will create the _id
field and assign it a unique ObjectId
value, as verified by the inserted document:mongod
将创建_id
字段,并为其分配一个唯一的ObjectId
值,由插入的文档验证:
The ObjectId
values are specific to the machine and time when the operation is run. ObjectId
值特定于机器和运行操作的时间。As such, your values may differ from those in the example.因此,您的值可能与示例中的值不同。
_id
Field_id
字段的文档¶In the following example, the document passed to the 在下面的示例中,传递给insert()
method includes the _id
field. insert()
方法的文档包含_id
字段。The value of _id
must be unique within the collection to avoid duplicate key error._id
的值在集合中必须是唯一的,以避免重复密钥错误。
The operation inserts the following document in the 该操作将在products
collection:products
集合中插入以下文档:
The following example performs a bulk insert of three documents by passing an array of documents to the 下面的示例通过向insert()
method. insert()
方法传递一个文档数组来执行三个文档的大容量插入。By default, MongoDB performs an ordered insert. 默认情况下,MongoDB执行有序插入。With ordered inserts, if an error occurs during an insert of one of the documents, MongoDB returns on error without processing the remaining documents in the array.对于有序插入,如果在插入其中一个文档时发生错误,MongoDB将返回错误,而不处理数组中的其余文档。
The documents in the array do not need to have the same fields. 数组中的文档不需要有相同的字段。For instance, the first document in the array has an 例如,数组中的第一个文档有一个_id
field and a type
field. _id
字段和一个type
字段。Because the second and third documents do not contain an 由于第二和第三个文档不包含_id
field, mongod
will create the _id
field for the second and third documents during the insert:_id
字段,mongod
将在插入过程中为第二和第三个文档创建_id
字段:
The operation inserted the following three documents:该操作插入了以下三个文档:
The following example performs an unordered insert of three documents. 以下示例执行三个文档的无序插入。With unordered inserts, if an error occurs during an insert of one of the documents, MongoDB continues to insert the remaining documents in the array.对于无序插入,如果插入其中一个文档时发生错误,MongoDB将继续在数组中插入其余文档。
The following operation to a replica set specifies a write concern of 以下对副本集的操作指定了一个写关注点"w: majority"
with a wtimeout
of 5000 milliseconds such that the method returns after the write propagates to a majority of the voting replica set members or the method times out after 5 seconds."w: majority"
,wtimeout
为5000毫秒,这样该方法在写入传播到大多数投票副本集成员后返回,或者该方法在5秒后超时。
When passed a single document, 当传递单个文档时,insert()
returns a WriteResult
object.insert()
返回WriteResult
对象。
The insert()
returns a WriteResult
object that contains the status of the operation. insert()
返回一个WriteResult
对象,该对象包含操作的状态。Upon success, the 成功后,WriteResult
object contains information on the number of documents inserted:WriteResult
对象包含有关插入的文档数的信息:
If the 如果insert()
method encounters write concern errors, the results include the WriteResult.writeConcernError
field:insert()
方法遇到写问题错误,结果包括WriteResult.writeConcernError
字段:
See also参阅
When passed an array of documents, 当传递一个文档数组时,insert()
returns a BulkWriteResult()
object. insert()
返回一个BulkWriteResult()
对象。See 有关详细信息,请参阅BulkWriteResult()
for details.BulkWriteResult()
。