On this page本页内容
$
¶The positional 定位$
operator identifies an element in an array to update without explicitly specifying the position of the element in the array.$
运算符标识数组中要更新的元素,而不显式指定元素在数组中的位置。
Disambiguation消歧
$
projection operator instead.$
运算符。$[]
instead.$[]
。$[<identifier>]
.$[<identifier>]
。The positional 定位$
operator has the form:$
的形式如下:
When used with update operations, e.g. 与更新操作一起使用时,例如db.collection.update()
and db.collection.findAndModify()
,db.collection.update()
和db.collection.findAndModify()
,
$
operator acts as a placeholder for the first element that matches the query document
, and$
运算符充当与查询文档匹配的第一个元素的占位符,并且array
field must appear as part of the query document
.array
字段必须作为查询文档的一部分出现。For example:例如:
upsert
¶Do not use the positional operator 不要将位置运算符$
with upsert operations because inserts will use the $
as a field name in the inserted document.$
用于upsert操作,因为inserts将使用$
作为插入文档中的字段名。
The positional 位置$
operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $
placeholder is a single value$
运算符不能用于遍历多个数组的查询,例如遍历嵌套在其他数组中的数组的查询,因为$
占位符的替换项是单个值
When used with the 当与$unset
operator, the positional $
operator does not remove the matching element from the array but rather sets it to null
.$unset
运算符一起使用时,定位$
运算符不会从数组中删除匹配元素,而是将其设置为null
。
If the query matches the array using a negation operator, such as 如果查询使用否定运算符(如$$ne
, $not
, or $nin
, then you cannot use the positional operator to update values from this array.$ne
、$not
或$nin
)匹配数组,则不能使用定位运算符更新此数组中的值。
However, if the negated portion of the query is inside of an 但是,如果查询的求反部分位于$elemMatch
expression, then you can use the positional operator to update this field.$elemMatch
表达式内,则可以使用位置运算符更新此字段。
Create a collection 使用以下文档创建一个集合students
with the following documents:students
:
To update the first element whose value is 要更新80
to 82
in the in the grades
array, use the positional $
operator if you do not know the position of the element in the array:grades
数组中值为80
到82
的第一个元素,如果不知道该元素在数组中的位置,请使用定位$
运算符:
Important
You must include the array field as part of the 必须将数组字段作为查询文档的一部分包含在内。query
document.
The positional 定位$
operator acts as a placeholder for the first match of the update query document.$
运算符充当更新查询文档的第一个匹配项的占位符。
After the operation, the 操作完成后,students
collection contains the following documents:students
集合包含以下文档:
The positional 定位$
operator facilitates updates to arrays that contain embedded documents. $
运算符有助于更新包含嵌入文档的数组。Use the positional 使用定位$
operator to access the fields in the embedded documents with the dot notation on the $
operator.$
运算符访问嵌入文档中的字段,并在$
运算符上使用点表示法。
Consider the following document in the 考虑students
collection whose grades
element value is an array of embedded documents:students
集合中的以下文档,其grades
元素值是嵌入文档的数组:
Use the positional 使用定位$
operator to update the std
field of the first array element that matches the grade
equal to 85
condition:$
运算符更新与grade
等于85
条件匹配的第一个数组元素的std
字段:
Important重要的
You must include the array field as part of the 必须将数组字段作为查询文档的一部分包含在内。query
document.
After the operation, the document has the following updated values:操作后,文档具有以下更新值:
The $
operator can update the first array element that matches multiple query criteria specified with the $elemMatch()
operator.$
运算符可以更新使用$elemMatch()
运算符指定的多个查询条件匹配的第一个数组元素。
Consider the following document in the 考虑students
collection whose grades
field value is an array of embedded documents:students
集合中的以下文档,其grades
字段值是嵌入文档的数组:
In the example below, the 在下面的示例中,$
operator updates the value of the std
field in the first embedded document that has grade
field with a value less than or equal to 90
and a mean
field with a value greater than 80
:$
运算符更新第一个嵌入文档中std
字段的值,该文档的grade
字段的值小于或等于90
,mean
字段的值大于80
:
This operation updates the first embedded document that matches the criteria, namely the second embedded document in the array:此操作将更新符合条件的第一个嵌入文档,即数组中的第二个嵌入文档: