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.$
运算符标识数组中要更新的元素,而不显式指定该元素在数组中的位置。
$
projection operator instead.$
投影运算符。$[]
instead.$[]
。$[<identifier>]
.$[<identifier>]
。The positional 位置$
operator has the form:$
运算符的形式为:
{ "<array>.$" : value }
When used with update operations, e.g. 当与更新操作一起使用时,例如db.collection.updateOne()
and db.collection.findAndModify()
,db.collection.updateOne()
和db.collection.findAndModify()
,
$
operator acts as a placeholder for the first element that matches the query document
, and$
运算符充当与query
文档匹配的第一个元素的占位符,并且array
field must appear as part of the query document
.array
字段必须显示为query
文档的一部分。For example:例如:
db.collection.updateOne( { <array>: value ... }, { <update operator>: { "<array>.$" : value } } )
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.有关详细信息,请参阅更新运算符行为。
upsert
Do not use the positional operator 不要在$
with upsert operations because inserts will use the $
as a field name in the inserted document.upsert
操作中使用位置运算符$
,因为插入将使用$
作为插入文档中的字段名。
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
运算符一起使用时,位置$
运算符不会从数组中删除匹配元素,而是将其设置为空。
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
表达式中,则可以使用位置运算符更新此字段。
The positional 在筛选多个数组字段时,位置$
update operator behaves ambiguously when filtering on multiple array fields.$
更新运算符的行为不明确。
When the server executes an update method, it first runs a query to determine which documents you want to update. 当服务器执行更新方法时,它首先运行查询以确定要更新哪些文档。If the update filters documents on multiple array fields, the subsequent call to the positional 如果更新筛选了多个数组字段上的文档,那么对位置$
update operator doesn't always update the required position in the array.$
更新运算符的后续调用并不总是更新数组中所需的位置。
Create a collection 使用以下文档创建students
with the following documents:students
集合:
db.students.insertMany( [ { "_id" : 1, "grades" : [ 85, 80, 80 ] }, { "_id" : 2, "grades" : [ 88, 90, 92 ] }, { "_id" : 3, "grades" : [ 85, 100, 90 ] } ] )
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
的第一个元素,如果不知道该元素在数组中的位置,请使用位置$
运算符:
You must include the array field as part of the 必须将数组字段作为query
document.query
文档的一部分。
db.students.updateOne( { _id: 1, grades: 80 }, { $set: { "grades.$" : 82 } } )
The positional 位置$
operator acts as a placeholder for the first match of the update query document.$
运算符充当更新query
文档的第一个匹配的占位符。
After the operation, the 操作后,students
collection contains the following documents:students
集合包含以下文档:
{ "_id" : 1, "grades" : [ 85, 82, 80 ] } { "_id" : 2, "grades" : [ 88, 90, 92 ] } { "_id" : 3, "grades" : [ 85, 100, 90 ] }
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.$
运算符访问嵌入文档中的字段,并在$
运算符上使用点符号。
db.collection.updateOne( { <query selector> }, { <update operator>: { "array.$.field" : value } } )
Consider the following document in the 考虑students
collection whose grades
element value is an array of embedded documents:students
集合中的以下文档,其grades
元素值是嵌入文档的数组:
{ _id: 4, grades: [ { grade: 80, mean: 75, std: 8 }, { grade: 85, mean: 90, std: 5 }, { grade: 85, mean: 85, std: 8 } ] }
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
字段:
You must include the array field as part of the 必须将数组字段作为query
document.query
文档的一部分。
db.students.updateOne( { _id: 4, "grades.grade": 85 }, { $set: { "grades.$.std" : 6 } } )
After the operation, the document has the following updated values:操作后,文档具有以下更新值:
{ "_id" : 4, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 8 }, { "grade" : 85, "mean" : 90, "std" : 6 }, { "grade" : 85, "mean" : 85, "std" : 8 } ] }
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
字段值是嵌入文档的数组:
{ _id: 5, grades: [ { grade: 80, mean: 75, std: 8 }, { grade: 85, mean: 90, std: 5 }, { grade: 90, mean: 85, std: 3 } ] }
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
:
db.students.updateOne( { _id: 5, grades: { $elemMatch: { grade: { $lte: 90 }, mean: { $gt: 80 } } } }, { $set: { "grades.$.std" : 6 } } )
This operation updates the first embedded document that matches the criteria, namely the second embedded document in the array:此操作更新符合条件的第一个嵌入文档,即数组中的第二个嵌入文档:
{ _id: 5, grades: [ { grade: 80, mean: 75, std: 8 }, { grade: 85, mean: 90, std: 6 }, { grade: 90, mean: 85, std: 3 } ] }
The positional 当查询具有多个数组字段以筛选集合中的文档时,位置$
update operator behaves ambiguously when the query has multiple array fields to filter documents in the collection.$
更新运算符的行为不明确。
Consider a document in the 考虑students_deans_list
collection, which holds arrays of student information:students_deans_list
集合中的一个文档,其中包含学生信息数组:
db.students_deans_list.insertMany( [ { _id: 8, activity_ids: [ 1, 2 ], grades: [ 90, 95 ], deans_list: [ 2021, 2020 ] } ] )
In the following example, the user attempts to modify the 在以下示例中,用户尝试修改deans_list
field, filtering documents using the activity_ids
, deans_list
, and grades
fields, and updating the 2021 value in the deans_list
field to 2022:deans_list
字段,使用activity_ids
、deans_list
和grades
字段筛选文档,并将deans_list
字段中的2021值更新为2022:
db.students_deans_list.updateOne( { activity_ids: 1, grades: 95, deans_list: 2021 }, { $set: { "deans_list.$": 2022 } } )
When the server executes the 当服务器执行上面的updateOne
method above, it filters the available documents using values in the supplied array fields. updateOne
方法时,它使用提供的数组字段中的值筛选可用文档。Although the 尽管筛选器中使用了deans_list
field is used in the filter, it is not the field used by the positional $
update operator to determine which position in the array to update:deans_list
字段,但它不是位置$
更新运算符用于确定要更新数组中的哪个位置的字段:
db.students_deans_list.find( { _id: 8 } )
Example output:示例输出:
{ _id: 8, activity_ids: [ 1, 2 ], grades: [ 90, 95 ], deans_list: [ 2021, 2022 ] }
The updateOne
method matched the deans_list
field on 2021, but the positional $
update operator instead changed the 2020 value to 2022.updateOne
方法在2021匹配了deans_list
字段,但位置$
更新运算符将2020值改为2022。
To avoid unexpected results when matching on multiple arrays, instead use the filtered positional operator 为了避免在多个数组上进行匹配时出现意外结果,请改用筛选的位置运算符$[<identifier>]
.$[<identifier>]
。