Docs HomeMongoDB Manual

Update Documents更新文档


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.


This page uses the following MongoDB Node.js Driver methods:此页面使用以下MongoDB Node.js驱动程序方法:

The examples on this page use the inventory collection. 此页面上的示例使用inventory集合。Connect to a test database in your MongoDB instance then create the inventory collection:连接到MongoDB实例中的测试数据库,然后创建inventory集合:

await db.collection('inventory').insertMany([
{
item: 'canvas',
qty: 100,
size: { h: 28, w: 35.5, uom: 'cm' },
status: 'A'
},
{
item: 'journal',
qty: 25,
size: { h: 14, w: 21, uom: 'cm' },
status: 'A'
},
{
item: 'mat',
qty: 85,
size: { h: 27.9, w: 35.5, uom: 'cm' },
status: 'A'
},
{
item: 'mousepad',
qty: 25,
size: { h: 19, w: 22.85, uom: 'cm' },
status: 'P'
},
{
item: 'notebook',
qty: 50,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'P'
},
{
item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'D'
},
{
item: 'planner',
qty: 75,
size: { h: 22.85, w: 30, uom: 'cm' },
status: 'D'
},
{
item: 'postcard',
qty: 45,
size: { h: 10, w: 15.25, uom: 'cm' },
status: 'A'
},
{
item: 'sketchbook',
qty: 80,
size: { h: 14, w: 21, uom: 'cm' },
status: 'A'
},
{
item: 'sketch pad',
qty: 95,
size: { h: 22.85, w: 30.5, uom: 'cm' },
status: 'A'
}
]);

Update Documents in a Collection更新集合中的文档

To update a document, MongoDB provides update operators such as $set to modify field values.为了更新文档,MongoDB提供了诸如$set之类的更新运算符来修改字段值。

To use the update operators, pass to the update methods an update document of the form:要使用更新运算符,请将以下表单的更新文档传递给更新方法:

{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}

Some update operators, such as $set, will create the field if the field does not exist. 如果字段不存在,某些更新运算符(如$set)将创建该字段。See the individual update operator reference for details.有关详细信息,请参阅单个更新运算符参考。

Note

Starting in MongoDB 4.2, MongoDB can accept an aggregation pipeline to specify the modifications to make instead of an update document. 从MongoDB 4.2开始,MongoDB可以接受一个聚合管道来指定要进行的修改,而不是更新文档。See the method reference page for details.有关详细信息,请参阅方法参考页。

Update a Single Document更新单个文档

The following example uses the Collection.updateOne() method on the inventory collection to update the first document where item equals "paper":以下示例对inventory集合使用Collection.updateOne()方法来更新item等于"paper"的第一个文档:

await db.collection('inventory').updateOne(
{ item: 'paper' },
{
$set: { 'size.uom': 'cm', status: 'P' },
$currentDate: { lastModified: true }
}
);

The update operation:更新操作:

  • uses the $set operator to update the value of the size.uom field to "cm" and the value of the status field to "P",使用$set运算符将size.uom字段的值更新为"cm",将status字段的值升级为"P"

  • uses the $currentDate operator to update the value of the lastModified field to the current date. 使用$currentDate运算符将lastModified字段的值更新为当前日期。If lastModified field does not exist, $currentDate will create the field. 如果lastModified字段不存在,则$currentDate将创建该字段。See $currentDate for details.有关详细信息,请参阅$currentDate

await db.collection('inventory').updateMany(
{ qty: { $lt: 50 } },
{
$set: { 'size.uom': 'in', status: 'P' },
$currentDate: { lastModified: true }
}
);

The update operation:更新操作:

  • uses the $set operator to update the value of the size.uom field to "in" and the value of the status field to "P",使用$set运算符将size.uom字段的值更新为"in",将status字段的值更改为"P"

  • uses the $currentDate operator to update the value of the lastModified field to the current date. 使用$currentDate运算符将lastModified字段的值更新为当前日期。If lastModified field does not exist, $currentDate will create the field. 如果lastModified字段不存在,则$currentDate将创建该字段。See $currentDate for details.有关详细信息,请参阅$currentDate

await db.collection('inventory').replaceOne(
{ item: 'paper' },
{
item: 'paper',
instock: [
{ warehouse: 'A', qty: 60 },
{ warehouse: 'B', qty: 40 }
]
}
);

Behavior行为

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和原子性的更多信息,请参阅原子性和事务

_id Field字段

Once set, you cannot update the value of the _id field nor can you replace an existing document with a replacement document that has a different _id field value.一旦设置,就不能更新_id字段的值,也不能用具有不同_id字段值的替换文档替换现有文档。

Field Order字段顺序

For write operations, MongoDB preserves the order of the document fields except for the following cases:对于写操作,MongoDB保留文档字段的顺序,但以下情况除外

  • The _id field is always the first field in the document._id字段始终是文档中的第一个字段。
  • Updates that include renaming of field names may result in the reordering of fields in the document.包括重命名字段名称的更新可能会导致文档中字段的重新排序。

Write Acknowledgement写入确认

With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.对于写入关注,您可以指定MongoDB为写操作请求的确认级别。有关详细信息,请参阅写入关注