$[]
On this page本页内容
Definition定义
$[]
-
The all positional operator所有位置运算符$[]
indicates that the update operator should modify all elements in the specified array field.$[]
表示更新运算符应修改指定数组字段中的所有元素。The$[]
operator has the following form:$[]
运算符具有以下形式:{ <update operator>: { "<array>.$[]" : value } }
Use in update operations, e.g.在更新操作中使用,例如db.collection.updateOne()
anddb.collection.findAndModify()
, to modify all array elements for the document or documents that match the query condition.db.collection.updateOne()
和db.collection.findAndModify()
,以修改文档或与查询条件匹配的文档的所有数组元素。For example:例如:db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)For an example, see Update All Elements in an Array.有关示例,请参阅更新数组中的所有元素。
Behavior行为
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. 从MongoDB 5.0开始,update运算符按照字典顺序处理具有基于字符串的名称的文档字段。Fields with numeric names are processed in numeric order. See Update Operators Behavior for details.具有数字名称的字段按数字顺序处理。有关详细信息,请参阅更新运算符行为。
upsert
If an upsert operation results in an insert, the 如果query
must include an exact equality match on the array field in order to use the $[]
positional operator in the update statement.upsert
操作导致插入,则query
必须在数组字段上包含完全相等的匹配项,才能在更新语句中使用$[]
位置运算符。
For example, the following upsert operation, which uses 例如,以下$[]
in the update document, specifies an exact equality match condition on the array field:upsert
操作在更新文档中使用$[]
,在数组字段上指定完全相等的匹配条件:
db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
If no such document exists, the operation would result in an insertion of the following document:如果不存在此类文件,操作将导致插入以下文件:
{ "_id" : ObjectId(...), "myArray" : [ 10, 10 ] }
If the upsert operation did not include an exact equality match and no matching documents were found to update, the upsert operation would error.如果upsert
操作不包括完全相等的匹配项,并且找不到要更新的匹配文档,则upsert
操作将出错。
For example, the following operations would error if no matching documents were found to update:例如,如果找不到要更新的匹配文档,则以下操作将出错:
db.emptyCollection.updateOne(
{ },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
db.emptyCollection.updateOne(
{ myArray: 5 },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
Nested Arrays嵌套数组
The $[]
operator can be used for queries that traverse more than one array and nested arrays.$[]
运算符可用于遍历多个数组和嵌套数组的查询。
For an example, see Update Nested Arrays in Conjunction with 有关示例,请参阅使用$[<identifier>]
.$[<identifier>]
更新嵌套数组。
Examples实例
Update All Elements in an Array更新数组中的所有元素
Create the 创建students
collection:students
集合:
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 82, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
To increment all elements in the 要将集合中所有文档的grades
array by 10
for all documents in the collection, use the all positional $[]
operator:grades
数组中的所有元素增加10
,请使用所有位置$[]
运算符:
db.students.updateMany(
{ },
{ $inc: { "grades.$[]": 10 } },
)
The all positional 所有位置$[]
operator acts as a placeholder for all elements in the array field.$[]
运算符充当数组字段中所有元素的占位符。
After the operation, the 操作后,students
collection contains the following documents:students
集合包含以下文档:
{ "_id" : 1, "grades" : [ 95, 92, 90 ] }
{ "_id" : 2, "grades" : [ 98, 100, 102 ] }
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }
Update All Documents in an Array更新数组中的所有文档
The $[]
positional operator facilitates updates to arrays that contain embedded documents. $[]
位置运算符便于更新包含嵌入文档的数组。To access the fields in the embedded documents, use the dot notation with the 若要访问嵌入文档中的字段,请使用带有$[]
operator.$[]
运算符的点表示法。
db.collection.updateOne(
{ <query selector> },
{ <update operator>: { "array.$[].field" : value } }
)
Create the 创建students2
collection:students2
集合:
db.students2.insertMany( [
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 8 },
{ "grade" : 85, "mean" : 90, "std" : 6 },
{ "grade" : 85, "mean" : 85, "std" : 8 }
]
},
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 8 },
{ "grade" : 87, "mean" : 90, "std" : 5 },
{ "grade" : 85, "mean" : 85, "std" : 6 }
]
}
] )
To modify the value of the 要修改std
field for all elements in the grades
array, use the positional $[]
operator:grades
数组中所有元素的std
字段的值,请使用位置$[]
运算符:
db.students2.updateMany(
{ },
{ $inc: { "grades.$[].std" : -2 } },
)
After the operation, the collection has the following documents:操作后,集合具有以下文档:
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 90, "std" : 4 },
{ "grade" : 85, "mean" : 85, "std" : 6 }
]
}
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 85, "mean" : 85, "std" : 4 }
]
}
Update Arrays Specified Using a Negation Query Operator更新使用否定查询运算符指定的数组
Create the 创建results
collection:results
集合:
db.results.insertMany( [
{ "_id" : 1, "grades" : [ 85, 82, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
To increment all elements in the 要将除grades
array by 10
for all documents except those with the value 100
in the grades
array, use the all positional $[]
operator:grades
数组中值为100
的文档外的所有文档的等级数组中的所有元素增加10
,请使用全部位置$[]
运算符:
db.results.updateMany(
{ "grades" : { $ne: 100 } },
{ $inc: { "grades.$[]": 10 } },
)
The all positional 全位置$[]
operator acts as a placeholder for all elements in the array field.$[]
运算符充当数组字段中所有元素的占位符。
After the operation, the 操作后,students
collection contains the following documents:students
集合包含以下文档:
{ "_id" : 1, "grades" : [ 95, 92, 90 ] }
{ "_id" : 2, "grades" : [ 98, 100, 102 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
Update Nested Arrays in Conjunction with $[<identifier>]
使用$[<identifier>]
更新嵌套数组
$[<identifier>]
The $[]
positional operator, in conjunction with filter $[<identifier>]
positional operator can be used to update nested arrays.$[]
位置运算符与筛选器$[<identifier>]
位置运算符可用于更新嵌套数组。
Create a collection 使用以下文档创建集合students3
with the following documents:students3
:
db.students3.insertMany( [
{ "_id" : 1,
"grades" : [
{ type: "quiz", questions: [ 10, 8, 5 ] },
{ type: "quiz", questions: [ 8, 9, 6 ] },
{ type: "hw", questions: [ 5, 4, 3 ] },
{ type: "exam", questions: [ 25, 10, 23, 0 ] },
]
}
] )
To update all values that are greater than or equal to 要更新嵌套8
in the nested grades.questions
array, regardless of type
:grades.questions
数组中大于或等于8
的所有值,无论type
如何:
db.students3.updateMany(
{},
{ $inc: { "grades.$[].questions.$[score]": 2 } },
{ arrayFilters: [ { "score": { $gte: 8 } } ] }
)
The updated documents look like this:更新后的文档如下所示:
{
_id: 1,
grades: [
{ type: 'quiz', questions: [ 12, 10, 5 ] },
{ type: 'quiz', questions: [ 10, 11, 6 ] },
{ type: 'hw', questions: [ 5, 4, 3 ] },
{ type: 'exam', questions: [ 27, 12, 25, 0 ] }
]
}