Update Documents更新文档
On this page本页内容
➤ 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 连接到MongoDB实例中的测试数据库,然后创建inventory
collection: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 为了更新文档,MongoDB提供了诸如$set
to modify field values.$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.有关详细信息,请参阅单个更新运算符参考。
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 thesize.uom
field to"cm"
and the value of thestatus
field to"P"
,$set
运算符将size.uom
字段的值更新为"cm"
,将status
字段的值升级为"P"
, -
uses the使用$currentDate
operator to update the value of thelastModified
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 thesize.uom
field to"in"
and the value of thestatus
field to"P"
,$set
运算符将size.uom
字段的值更新为"in"
,将status
字段的值更改为"P"
, -
uses the使用$currentDate
operator to update the value of thelastModified
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为写操作请求的确认级别。有关详细信息,请参阅写入关注。