更新 MongoDB 中的多个文档
-
MongoDB 中的
updateMany()方法 -
MongoDB 中的
$set运算符 - 在 MongoDB 中创建要更新的集合
-
使用
updateMany()方法更新 MongoDB 中的多个文档 -
使用
updateMany()方法更新 MongoDB 中的嵌入式文档 -
使用
updateMany()方法更新 MongoDB 中的数组元素 - 结论
本文将讨论如何高效地更新 MongoDB 中的多个文档。
MongoDB 中的 updateMany() 方法
使用 MongoDB 中的 db.collection.updateMany() 方法,你可以更新集合中的多个文档。此方法更改与指定过滤器匹配的所有集合文档。
以下是使用此方法时的一些注意事项。
- 此方法仅接受具有更新的运算符表达式的文档。
updateMany()方法可用于多文档事务。- 当你更新文档时,
_id字段的值不会改变。 - 它还为文档添加了新字段。
语法:
db.collection.updateMany(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, <filterdocument2>... ],
hint: <document|string>
}
)
参数:
filter:这是方法的第一个参数。它指定更新的选择标准。
此参数的类型是文档。如果它包含一条空记录,即 {},则此方法将使用更新后的文档更新集合的所有文档。
-
update:这是方法的第二个参数。此参数类型是文档或管道,它包含对文档的更改。它可以是更新的文档(包含编辑运算符表达式)或聚合管道(仅包含聚合步骤,例如
$addFields、$project和$replaceRoot)。
可选参数:
-
upsert:此参数的值为真或假。假设此参数的值为true。该过程将更新满足该方案中指定标准的所有文档。如果集合中的任何文档不适合提供的过滤器,此方法将在集合中输入新文档(即更新的文档)。
此选项的默认值为
false,其类型为布尔值。 -
writeConcern:当你不想使用默认的写入关注点时,这是唯一的选择。此选项具有参数类型文档。 -
collation:它描述排序规则将如何在操作中使用。它还允许用户建立特定于语言的字符串比较标准,例如小写和重音标记规则。此选项具有文档的参数类型。
-
arrayFilters:这是一个过滤器文档数组,告诉你要更改哪些数组元素以进行数组字段更新。该参数具有数组类型。 -
hint:它是一个文档或字段,指示过滤器将用于支持它的索引。它可以接受索引规范文档或索引名称字符串,如果你选择不存在的索引,它将返回错误。 -
Return: 此方法将返回一个布尔值为true(如果writeconcern启用)或false(如果writeconcern禁用)的文档,匹配文档数量的matchedCount值,modifiedCount修改记录数的值,以及更新插入文档的_id的upsertedId值。
MongoDB 中的 $set 运算符
你通常使用 $set 运算符来形成更新参数。它用用户给定的值替换字段的值。
语法:
{ $set: { <field1>: <value1>, <field2>: <value2>, ...}}
假设 field 不存在。然后,$set 运算符将使用记录中提供的值创建一个新字段。
如果你使用点符号定义 field,例如 embededDoc.field,,并且数据库中不存在 field,$set 运算符会创建嵌入文档。
在 MongoDB 中创建要更新的集合
你可以使用以下名为 products 的集合。
db={
"products": [
{
"_id": 1,
"name": "xPhone",
"price": 799,
"specs": {
"ram": 4,
"screen": 6.5,
"cpu": 2.66
},
"color": [
"white",
"black"
],
"storage": [
64,
128,
256
]
},
{
"_id": 2,
"name": "xTablet",
"price": 899,
"specs": {
"ram": 16,
"screen": 9.5,
"cpu": 3.66
},
"color": [
"white",
"black",
"purple"
],
"storage": [
128,
256,
512
]
},
{
"_id": 3,
"name": "SmartTablet",
"price": 899,
"specs": {
"ram": 12,
"screen": 9.7,
"cpu": 3.66
},
"color": [
"blue"
],
"storage": [
16,
64,
128
]
},
{
"_id": 4,
"name": "SmartPad",
"price": 699,
"specs": {
"ram": 8,
"screen": 9.7,
"cpu": 1.66
},
"color": [
"white",
"orange",
"gold",
"gray"
],
"storage": [
128,
256,
1024
]
},
{
"_id": 5,
"name": "SmartPhone",
"price": 599,
"specs": {
"ram": 4,
"screen": 5.7,
"cpu": 1.66
},
"color": [
"white",
"orange",
"gold",
"gray"
],
"storage": [
128,
256
]
}
]
}
使用 updateMany() 方法更新 MongoDB 中的多个文档
以下示例中使用 updateMany() 方法在价格字段值为 899. 时更新记录。
db.products.updateMany(
{ price: 899},
{ $set: { price: 895 }}
)
此查询中的 filter 输入是 {price: 899},,它指定要更新的文档。要应用的更改是 {$set: { price: 895}},它利用 set 运算符将 price 字段值分配给 895.。
输出:

该结果文档中的 matchedCount 字段表示匹配记录的数量,而 modifiedCount 字段存储更新文档的数量。
为了验证修改,使用 find() 方法选择 price 字段值为 895, 的文档,如下所示:
db.products.find({
price: 895
}, {
name: 1,
price: 1
})
输出:

上图显示 price 字段值已成功更新。
使用 updateMany() 方法更新 MongoDB 中的嵌入式文档
以下查询使用 find() 方法来发现 price 字段值大于 700 的文档。
db.products.find({
price: { $gt: 700}
}, {
name: 1,
price: 1,
spec: 1
})
输出:

updateMany() 方法用于这些文档的 spec 嵌入式文档中,用于更新 ram、screen 和 cpu 字段的值:
db.products.updateMany({
price: { $gt: 700}
}, {
$set: {
"spec.ram": 32,
"spec.screen": 9.8,
"spec.cpu": 5.66
}
})
输出:

图像中的结果表明三个文档已成功更新。
使用 updateMany() 方法更新 MongoDB 中的数组元素
updateMany() 方法在以下示例中用于更新 storage 文档数组的第一个和第二个元素,其中 _id 为 1、2 和 3。
db.products.updateMany({
_id: {
$in: [1, 2, 3]
}
}, {
$set: {
"storage.0": 16,
"storage.1": 32
}
})
输出:

如果你从 products 集合中查询 _id 为 1、2 和 3 的文档,storage 数组的第一个和第二个组件已被修改。
db.products.find(
{ _id: { $in: [1,2,3]}},
{ name: 1, storage: 1}
)
输出:

结论
通过本教程文章,你学习了如何使用 updateMany() 函数更新集合中满足条件的所有记录,并使用 $set 运算符将字段的值替换为特定值。