On this page本页内容
$[]
¶New in version 3.6.版本3.6中的新功能。
The all positional operator 所有位置运算符$[]
indicates that the update operator should modify all elements in the specified array field.$[]
表示更新运算符应修改指定数组字段中的所有元素。
The $[]
operator has the following form:$[]
运算符的形式如下:
Use in update operations, e.g. 用于更新操作,例如db.collection.update()
and db.collection.findAndModify()
, to modify all array elements for the document or documents that match the query condition. db.collection.update()
和db.collection.findAndModify()
方法,用于修改文档或与查询条件匹配的文档的所有数组元素。For example:例如:
For an example, see Update All Elements in an Array.有关示例,请参阅更新数组中的所有元素。
upsert
¶If an upsert operation results in an insert, the 如果upsert操作导致插入,则query
must include an exact equality match on the array field in order to use the $[]
positional operator in the update statement.query
必须在数组字段上包含完全相等的匹配,以便在更新语句中使用$[]
位置运算符。
For example, the following upsert operation, which uses 例如,以下upsert操作在更新文档中使用$[]
in the update document, specifies an exact equality match condition on the array field:$[]
指定数组字段上的精确相等匹配条件:
If no such document exists, the operation would result in an insertion of the following document:如果不存在此类文档,操作将导致插入以下文档:
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 $[]
operator can be used for queries which traverse more than one array and nested arrays.$[]
运算符可用于遍历多个数组和嵌套数组的查询。
For an example, see Update Nested Arrays in Conjunction with $[<identifier>].有关示例,请参阅结合$[<identifier>]
更新嵌套数组。
Consider a collection 考虑集合students
with the following documents:students
带有下列文档:
To increment all elements in the 要对集合中的所有文档将grades
array by 10
for all documents in the collection, use the all positional $[]
operator: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
集合包含以下文档:
The $[]
positional operator facilitates updates to arrays that contain embedded documents. $[]
位置运算符有助于更新包含嵌入文档的数组。To access the fields in the embedded documents, use the dot notation on the 要访问嵌入文档中的字段,请在$[]
operator.$[]
运算符上使用点符号。
Consider a collection 考虑一个集合students2
with the following documents:students2
带有以下文档:
To modify the value of the 要修改std
field for all elements in the grades
array, use the positional $[]
operator:grades
数组中所有元素的std
字段值,请使用位置$[]
运算符:
After the operation, the collection has the following documents:操作完成后,集合具有以下文档:
Consider a collection 考虑集合results
with the following documents:results
带有以下文档:
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
的文档之外的所有文档的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
集合包含以下文档:
$[<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
集合带有以下文档:
To update all values that are greater than or equal to 更新嵌套8
in the nested grades.questions
array, regardless of type
:grades.questions
数组中大于或等于8
的所有值,无论类型: