On this page本页内容
$[<identifier>]
¶New in version 3.6.版本3.6中的新功能。
The filtered positional operator 筛选位置运算符$[<identifier>]
identifies the array elements that match the arrayFilters conditions for an update operation, e.g. db.collection.update()
and db.collection.findAndModify()
.$[<identifier>]
标识与arrayFilters
条件相匹配的数组元素用于更新操作,例如db.collection.update()
和db.collection.findAndModify()
。
Used in conjunction with the 与arrayFilters
option, the $[<identifier>]
operator has the following form:arrayFilters
选项一起使用,$[<identifier>]
运算符的形式如下:
Use in conjunction with the 与arrayFilters
option to update all elements that match the arrayFilters conditions in the document or documents that match the query conditions. arrayFilters
选项结合使用,以更新文档中与arrayFilters
条件匹配的所有元素或与查询条件匹配的文档。For example:例如:
Note
The <identifier>
must begin with a lowercase letter and contain only alphanumeric characters.<identifier>
必须以小写字母开头,并且只包含字母数字字符。
For an example, see Update All Array Elements That Match arrayFilters.有关示例,请参阅更新与ArrayFilter
匹配的所有数组元素。
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
操作导致插入,则query
必须在数组字段中包含完全相等的匹配项,以便在更新语句中使用$[<identifier>]
。
For example, the following upsert operation, which uses 例如,以下upsert操作在更新文档中使用 $[<identifier>]
in the update document, specifies an exact equality match condition on the array field:$[<identifier>]
在数组字段上指定精确的相等匹配条件:
If no such document exists, the operation would result in an insert of a document that resembles the following:如果不存在此类文件,操作将导致插入类似以下内容的文件:
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:例如,如果没有找到要更新的匹配文档,则以下操作将出错:
The operation would return an error that resembles the following:该操作将返回一个类似于以下内容的错误:
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 $[].有关示例,请参阅结合$[]
更新嵌套数组。
arrayFilters
arrayFilters
匹配的所有数组元素¶Consider a collection 考虑一个集合students
with the following documents:students
带有下列文档:
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
一起使用:
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
集合包含以下文档:
arrayFilters
in an ArrayarrayFilters
匹配的所有文档¶The $[<identifier>]
operator facilitates updates to arrays that contain embedded documents. $[<identifier>]
运算符有助于更新包含嵌入式文档的数组。To access the fields in the embedded documents, use the dot notation on the 要访问嵌入文档中的字段,请在$[<identifier>]
.$[<identifier>]
上使用点符号。
Consider a collection 考虑一个集合students2
with the following documents:students2
具有以下文档:
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:grade
大于或等于85
的grades
数组中所有元素的mean
字段,请使用位置运算符$[<identifier>]
和arrayFilters
:
After the operation, the collection has the following documents:操作完成后,集合具有以下文档:
Consider a collection 考虑一个集合students2
with the following documents:students2
带有以下文档:
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
数组中grades
大于或等于80
且std
大于或等于5
的所有元素的std
字段值,请使用位置运算符$[<identifier>]
和arrayFilters
:
After the operation, the collection has the following documents:操作完成后,集合具有以下文档:
Consider a collection 考虑一个集合alumni
with the following documents:alumni
带有以下文档:
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
查询运算符:
After the operation, the collection has the following documents:操作完成后,集合具有以下文档:
$[]
$[]
更新嵌套数组¶The $[<identifier>]
filtered positional operator, in conjunction with the $[]
all positional operator, can be used to update nested arrays.$[<identifier>]
筛选的位置运算符与$[]
全部位置运算符一起可用于更新嵌套数组。
Create a collection 创建一个集合students3
with the following document:students3
带有以下文档:
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.questions
数组中大于或等于8
的值,如果相关联的grades.type
字段是quiz
。
After the operation, the collection has the following document:操作完成后,集合具有以下文档:
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
是什么: