$[<identifier>]
On this page本页内容
Definition定义Behavior行为Restrictions限制upsert
Nested Arrays嵌套数组Examples实例Update All Array Elements That Match更新与arrayFilters
arrayFilters
匹配的所有数组元素Update All Documents That Match更新与数组中的arrayFilters
in an ArrayarrayFilters
匹配的所有文档Update All Array Elements that Match Multiple Conditions更新匹配多个条件的所有数组元素Update Array Elements Using a Negation Operator使用求反运算符更新数组元素Update Nested Arrays in Conjunction with与$[]
$[]
一起更新嵌套数组
Definition定义
$[<identifier>]
-
The filtered positional operator已筛选的位置运算符$[<identifier>]
identifies the array elements that match thearrayFilters
conditions for an update operation, e.g.db.collection.updateMany()
anddb.collection.findAndModify()
.$[<identifier>]
标识与更新操作的arrayFilters
条件匹配的数组元素,例如db.collection.updateMany()
和db.collection.findAndModify()
。Used in conjunction with thearrayFilters
option, the$[<identifier>]
operator has the following form:$[<identifier>]
运算符与arrayFilters
选项一起使用,其形式如下:{ <update operator>: { "<array>.$[<identifier>]" : value } },
{ arrayFilters: [ { <identifier>: <condition> } ] }Use in conjunction with the与arrayFilters
option to update all elements that match thearrayFilters
conditions in the document or documents that match the query conditions.arrayFilters
选项一起使用,可以更新文档中与arrayFilters
条件匹配的所有元素或与查询条件匹配的文档。For example:例如:db.collection.updateMany(
{ <query conditions> },
{ <update operator>: { "<array>.$[<identifier>]" : value } },
{ arrayFilters: [ { <identifier>: <condition> } ] }
)NoteThe<identifier>
must begin with a lowercase letter and contain only alphanumeric characters.<identifier>
必须以小写字母开头,并且仅包含字母数字字符。For an example, see Update All Array Elements That Match有关示例,请参阅更新与arrayFilters
.arrayFilters
匹配的所有数组元素。
Behavior行为
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. 从MongoDB 5.0开始,更新运算符按照字典顺序处理具有基于字符串的名称的文档字段。Fields with numeric names are processed in numeric order. 具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为。
Restrictions限制
The arrayFilters
option cannot include the following query operators:arrayFilters
选项不能包含以下查询运算符:
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 $[<identifier>]
in the update statement.upsert
操作导致插入,则查询必须在数组字段上包含完全相等的匹配项,才能在更新语句中使用$[<identifier>]
。
For example, the following upsert operation, which uses 例如,以下upstart操作在更新文档中使用$[<identifier>]
in the update document, specifies an exact equality match condition on the array field:$[<identifier>]
,它在数组字段上指定了一个完全相等的匹配条件:
db.collection.updateOne(
{ myArray: [ 0, 1 ] },
{ $set: { "myArray.$[element]": 2 } },
{ arrayFilters: [ { element: 0 } ], upsert: true }
)
If no such document exists, the operation would result in an insert of a document that resembles the following:如果不存在此类文件,操作将导致插入类似于以下内容的文件:
{ "_id" : ObjectId(...), "myArray" : [ 2, 1 ] }
If the upsert operation did not include an exact equality match and no matching documents were found to update, the upsert operation would error. For example, the following operations would error if no matching documents were found to update:如果追加销售操作不包括完全相等的匹配项,并且找不到要更新的匹配文档,则追加销售操作将出错。例如,如果找不到要更新的匹配文档,则以下操作将出错:
db.array.updateOne(
{ },
{ $set: { "myArray.$[element]": 10 } },
{ arrayFilters: [ { element: 9 } ], upsert: true }
)
The operation would return an error that resembles the following:该操作将返回类似于以下内容的错误:
MongoServerError: The path 'myArray' must exist in the document in order to apply array updates.
Nested Arrays嵌套数组
The filtered positional operator 筛选后的位置运算符$[<identifier>]
can be used for queries which traverse more than one array and nested arrays.$[<identifier>]
可用于遍历多个数组和嵌套数组的查询。
For an example, see Update Nested Arrays in Conjunction with 有关示例,请参阅使用$[]
.$[]
更新嵌套数组。
Examples实例
Update All Array Elements That Match arrayFilters
更新与arrayFilters
匹配的所有数组元素
arrayFilters
Create the 创建students
collection:students
集合:
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 95, 92, 90 ] },
{ "_id" : 2, "grades" : [ 98, 100, 102 ] },
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }
] )
To update all elements that are greater than or equal to 要更新100
in the grades
array, use the filtered positional operator $[<identifier>]
with the arrayFilters
:grades
数组中大于或等于100
的所有元素,请将筛选后的位置运算符$[<identifier>]
与arrayFilters
一起使用:
db.students.updateMany(
{ },
{ $set: { "grades.$[element]" : 100 } },
{ arrayFilters: [ { "element": { $gte: 100 } } ] }
)
The positional 位置$[<identifier>]
operator acts as a placeholder for all elements in the array field that match the conditions specified in arrayFilters
.$[<identifier>]
运算符充当数组字段中与arrayFilters
中指定的条件匹配的所有元素的占位符。
After the operation, the 操作后,students
collection contains the following documents:students
集合包含以下文档:
{ "_id" : 1, "grades" : [ 95, 92, 90 ] }
{ "_id" : 2, "grades" : [ 98, 100, 100 ] }
{ "_id" : 3, "grades" : [ 95, 100, 100 ] }
Update All Documents That Match arrayFilters
in an Array更新与数组中的arrayFilters
匹配的所有文档
arrayFilters
in an ArrayThe $[<identifier>]
operator facilitates updates to arrays that contain embedded documents. $[<identifier>]
运算符便于更新包含嵌入文档的数组。To access the fields in the embedded documents, use the dot notation with the 要访问嵌入文档中的字段,请使用带有$[<identifier>]
.$[<identifier>]
的点表示法。
db.collection.updateMany(
{ <query selector> },
{ <update operator>: { "array.$[<identifier>].field" : value } },
{ arrayFilters: [ { <identifier>: <condition> } } ] }
)
Create the 创建students2
collection:students2
集合:
db.students2.insertMany( [
{
"_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 }
]
}
] )
To modify the value of the 要修改mean
field for all elements in the grades
array where the grade is greater than or equal to 85
, use the positional $[<identifier>]
operator and arrayFilters
:grades
数组中grade
大于或等于85
的所有元素的mean
字段值,请使用位置$[<identifier>]
运算符和arrayFilters
:
db.students2.updateMany(
{ },
{ $set: { "grades.$[elem].mean" : 100 } },
{ arrayFilters: [ { "elem.grade": { $gte: 85 } } ] }
)
After the operation, the collection has the following documents:操作后,集合具有以下文档:
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 100, "std" : 4 },
{ "grade" : 85, "mean" : 100, "std" : 6 }
]
}
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 100, "std" : 6 },
{ "grade" : 87, "mean" : 100, "std" : 3 },
{ "grade" : 85, "mean" : 100, "std" : 4 }
]
}
Update All Array Elements that Match Multiple Conditions更新匹配多个条件的所有数组元素
Create the 创建students3
collection:students3
集合:
db.students3.insertMany( [
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 100, "std" : 4 },
{ "grade" : 85, "mean" : 100, "std" : 6 }
]
},
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 100, "std" : 6 },
{ "grade" : 87, "mean" : 100, "std" : 3 },
{ "grade" : 85, "mean" : 100, "std" : 4 }
]
}
] )
To modify the value of the 要修改std
field for all elements in the grades
array where both the grade is greater than or equal to 80
and the std
is greater than or equal to 5
, use the positional $[<identifier>]
operator and arrayFilters
:grades
数组中grade
大于或等于80
且std
大于或等于5
的所有元素的std
字段的值,请使用位置$[<identifier>]
运算符和arrayFilters
:
db.students3.updateMany(
{ },
{ $inc: { "grades.$[elem].std" : -1 } },
{ arrayFilters: [ { "elem.grade": { $gte: 80 }, "elem.std": { $gt: 5 } } ] }
)
After the operation, the collection has the following documents:操作后,集合具有以下文档:
{ "_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 5 },
{ "grade" : 85, "mean" : 100, "std" : 4 },
{ "grade" : 85, "mean" : 100, "std" : 5 }
]
}
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 100, "std" : 5 },
{ "grade" : 87, "mean" : 100, "std" : 3 },
{ "grade" : 85, "mean" : 100, "std" : 4 }
]
}
Update Array Elements Using a Negation Operator使用求反运算符更新数组元素
Create the 创建alumni
collection:alumni
集合:
db.alumni.insertMany( [
{
"_id": 1,
"name": "Christine Franklin",
"degrees": [
{ "level": "Master" },
{ "level": "Bachelor" }
],
},
{
"_id": 2,
"name": "Reyansh Sengupta",
"degrees": [ { "level": "Bachelor" } ],
}
] )
To modify all elements in the 要修改degrees
array that do not have "level": "Bachelor"
, use the positional $[<identifier>]
operation with the $ne
query operator:degrees
数组中没有"level": "Bachelor"
的所有元素,请将位置$[<identifier>]
操作与$ne
查询运算符一起使用:
db.alumni.updateMany(
{ },
{ $set : { "degrees.$[degree].gradcampaign" : 1 } },
{ arrayFilters : [ {"degree.level" : { $ne: "Bachelor" } } ] }
)
After the operation, the collection has the following documents:操作后,集合具有以下文档:
{
_id: 1,
name: 'Christine Franklin',
degrees: [
{ level: 'Master', gradcampaign: 1 },
{ level: 'Bachelor' }
]
},
{
_id: 2,
name: 'Reyansh Sengupta',
degrees: [ { level: 'Bachelor' } ]
}
Update Nested Arrays in Conjunction with $[]
与$[]
一起更新嵌套数组
$[]
The $[<identifier>]
filtered positional operator, in conjunction with the $[]
all positional operator, can be used to update nested arrays.$[<identifier>]
筛选的位置运算符与所有位置运算符$[]
可用于更新嵌套数组。
Create a collection 使用以下文档创建集合students4
with the following document:students4
:
db.students4.insertOne(
{ "_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 ] },
]
}
)
The following updates the values that are greater than or equal to 如果关联的8
in the nested grades.questions
array if the associated grades.type
field is quiz
.grades.type
字段为quiz
,则以下内容将更新嵌套grades.questions
数组中大于或等于8
的值。
db.students4.updateMany(
{},
{ $inc: { "grades.$[t].questions.$[score]": 2 } },
{ arrayFilters: [ { "t.type": "quiz" }, { "score": { $gte: 8 } } ] }
)
.. note::
The spacing is significant in the array identifier. If you write
the identifier as ``grades.$[ t ].questions.$[ score ]``, the
operation will fail.
After the operation, the collection has the following document:操作后,集合具有以下文档:
{
"_id" : 1,
"grades" : [
{ "type" : "quiz", "questions" : [ 12, 10, 5 ] },
{ "type" : "quiz", "questions" : [ 10, 11, 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.students4.updateMany(
{},
{ $inc: { "grades.$[].questions.$[score]": 2 } },
{ arrayFilters: [ { "score": { $gte: 8 } } ] }
)