Insert Documents插入文档
➤ Use the Select your language drop-down menu in the upper-right to set the language of the examples on this page.
This page provides examples of insert operations in MongoDB.本页提供了MongoDB中插入操作的示例。
Creating a Collection创建集合
If the collection does not currently exist, insert operations will create the collection.如果该集合当前不存在,则插入操作将创建该集合。
Insert a Single Document插入单个文档
Collection.insertOne() inserts a single document into a collection.Collection.insertOne()
将单个文档插入到集合中。
The following example inserts a new document into the 以下示例将一个新文档插入到inventory
collection. inventory
集合中。If the document does not specify an 如果文档没有指定_id
field, the Node.js driver adds the _id
field with an ObjectId value to the new document. _id
字段,Node.js驱动程序会将具有ObjectId值的_id
字段添加到新文档中。See Insert Behavior.请参见插入行为。
await db.collection('inventory').insertOne({
item: 'canvas',
qty: 100,
tags: ['cotton'],
size: { h: 28, w: 35.5, uom: 'cm' }
});
insertOne() returns a promise that provides a 返回一个提供result
. The result.insertedId
promise contains the _id
of the newly inserted document.result
的promise。result.insertedId
promise包含新插入文档的_id
。
To retrieve the document that you just inserted, query the collection:若要检索刚刚插入的文档,请查询集合:
const cursor = db.collection('inventory').find({ item: 'canvas' });
Insert Multiple Documents插入多个文档
➤ Use the Select your language drop-down menu in the upper-right to set the language of the examples on this page.
Collection.insertMany()
can insert multiple documents into a collection. 可以将多个文档插入到集合中。Pass an array of documents to the method.将文档数组传递给该方法。
The following example inserts three new documents into the 以下示例将三个新文档插入到inventory
collection. inventory
集合中。If the documents do not specify an 如果文档没有指定_id
field, the Node.js driver adds the _id
field with an ObjectId value to each document. _id
字段,Node.js驱动程序会向每个文档添加带有ObjectId值的_id
字段。See Insert Behavior.请参见插入行为。
await db.collection('inventory').insertMany([
{
item: 'journal',
qty: 25,
tags: ['blank', 'red'],
size: { h: 14, w: 21, uom: 'cm' }
},
{
item: 'mat',
qty: 85,
tags: ['gray'],
size: { h: 27.9, w: 35.5, uom: 'cm' }
},
{
item: 'mousepad',
qty: 25,
tags: ['gel', 'blue'],
size: { h: 19, w: 22.85, uom: 'cm' }
}
]);
insertMany()
returns a promise that provides a 返回一个提供result
. result
的promise。The result.insertedIds
field contains an array with the _id
of each newly inserted document.result.insertedIds
字段包含一个数组,其中包含每个新插入文档的_id
。
To retrieve the inserted documents, query the collection:要检索插入的文档,请查询集合:
const cursor = db.collection('inventory').find({});
Insert Behavior插入行为
Collection Creation集合创建
If the collection does not currently exist, insert operations will create the collection.如果该集合当前不存在,则插入操作将创建该集合。
_id
Field字段
In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. 在MongoDB中,存储在集合中的每个文档都需要一个唯一的_id
字段作为主键。If an inserted document omits the 如果插入的文档省略了_id
field, the MongoDB driver automatically generates an ObjectId for the _id
field._id
字段,MongoDB驱动程序会自动为_id
字段生成一个ObjectId。
This also applies to documents inserted through update operations with upsert: true.这也适用于通过upsert:true
的更新操作插入的文档。
Atomicity原子性
All write operations in MongoDB are atomic on the level of a single document. MongoDB中的所有写操作都是单个文档级别上的原子操作。For more information on MongoDB and atomicity, see Atomicity and Transactions有关MongoDB和原子性的更多信息,请参阅原子性和事务
Write Acknowledgement写入确认
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. 对于写问题,您可以指定MongoDB为写操作请求的确认级别。For details, see Write Concern.有关详细信息,请参阅写入关注。