On this page本页内容
$
¶The positional 条件$
operator limits the contents of an <array>
to return either:$
运算符限制<array>
的内容返回:
Use 在所选文档中只需要一个特定数组元素时,在$
in the projection document of the find()
method or the findOne()
method when you only need one particular array element in selected documents.find()
方法或findOne()
方法的投影文档中使用$
。
See the aggregation operator 请参阅聚合运算符$filter
to return an array with only those elements that match the specified condition.$filter
,以返回仅包含与指定条件匹配的元素的数组。
Disambiguation消除歧义
To specify an array element to update, see the positional $ operator for updates.要指定要更新的数组元素,请参阅位置$运算符以获取更新。
Both the $
operator and the $elemMatch
operator project the first matching element from an array based on a condition.$
运算符和$elemMatch
运算符都根据条件从数组中投影第一个匹配元素。
The $
operator projects the first matching array element from each document in a collection based on some condition from the query statement.$
运算符根据查询语句中的某些条件,从集合中的每个文档中投影第一个匹配的数组元素。
The $elemMatch
projection operator takes an explicit condition argument. $elemMatch
投影运算符接受一个显式条件参数。This allows you to project based on a condition not in the query, or if you need to project based on multiple fields in the array’s embedded documents. 这允许您基于不在查询中的条件进行投影,或者如果需要基于数组嵌入文档中的多个字段进行投影。See Array Field Limitations for an example.有关示例,请参阅数组字段限制。
视图上的db.collection.find()
operations on views do not support $
projection operator.db.collection.find()
操作不支持$
投影运算符。
To return the first array element that matches the specified query condition on the array:要返回与数组上指定的查询条件匹配的第一个数组元素,请执行以下操作:
Changed in version 4.4.在版本4.4中更改。You can use the 可以使用$
operator to limit an <array>
field which does not appear in the query document. $
运算符限制查询文档中未出现的<array>
字段。In previous versions of MongoDB, the 在以前版本的MongoDB中,被限制的<array>
field being limited must appear in the query document.<array>
字段必须出现在查询文档中。
Important重要的
To ensure expected behavior, the arrays used in the query document and the projection document must be the same length. 为了确保预期的行为,查询文档和投影文档中使用的数组长度必须相同。If the arrays are different lenghts, the operation may error in certain scenarios.如果阵列长度不同,则在某些情况下操作可能会出错。
MongoDB requires the following when dealing with projection over arrays:在处理阵列上的投影时,MongoDB要求:
$
operator may appear in the projection document.$
运算符。For example, the following projection may lead to undefined behavior:例如,以下投影可能会导致未定义的行为:
To specify criteria on multiple fields of documents inside that array, use the 要在该数组中的多个文档字段上指定条件,请使用$elemMatch
query operator. $elemMatch
查询运算符。The following query returns the first document inside a 以下查询返回grades
array that has a mean
of greater than 70 and a grade
of greater than 90.mean
值大于70且grade
大于90的grades
数组中的第一个文档。
You must use the 如果选择文档和选择这些文档中的字段需要单独的条件,则必须使用$elemMatch
operator if you need separate conditions for selecting documents and for choosing fields within those documents.$elemMatch
运算符。
When the 当find()
method includes a sort()
, the find()
method applies the sort()
to order the matching documents before it applies the positional $
projection operator.find()
方法包含sort()
时,find()
方法在应用位置$
投影运算符之前应用sort()
对匹配文档进行排序。
If an array field contains multiple documents with the same field name and the 如果数组字段包含多个具有相同字段名的文档,并且find()
method includes a sort()
on that repeating field, the returned documents may not reflect the sort order because the sort was applied to the elements of the array before the $
projection operator.find()
方法在该重复字段上包含sort()
,则返回的文档可能不会反映排序顺序,因为排序是在$
投影运算符之前应用于数组元素的。
Starting in MongoDB 4.4, the 从MongoDB 4.4开始,$
projection operator can only appear at the end of the field path; e.g. "field.$"
or "fieldA.fieldB.$"
.$
投影运算符只能出现在字段路径的末尾;亦即"field.$"
或"fieldA.fieldB.$"
。
For example, starting in MongoDB 4.4, the following operation is invalid:例如,从MongoDB 4.4开始,以下操作无效:
To resolve, remove the component of the field path that follows the 要解决此问题,请删除$
projection operator.$
投影运算符后面的字段路径的组件。
In previous versions, MongoDB ignores the part of the path that follows the 在以前的版本中,MongoDB会忽略跟随$
; i.e. the projection is treated as "instock.$"
.$
的路径部分;亦即,投影被视为"instock.$"
。
$slice
Restriction$slice
限制¶Starting in MongoDB 4.4, 从MongoDB 4.4开始,find
and findAndModify
projection cannot include $slice
projection expression as part of a $
projection expression.find
和findAndModify
投影不能将$slice
投影表达式作为$
投影表达式的一部分。
For example, starting in MongoDB 4.4, the following operation is invalid:例如,从MongoDB 4.4开始,以下操作无效:
MongoDB already has a MongoDB已经有一个限制,顶级字段名不能以美元符号(restriction
where top-level field names cannot start with the dollar sign ($
).$
)开头。
In previous versions, MongoDB returns the first element (在以前的版本中,MongoDB返回instock.$
) in the instock
array that matches the query condition; i.e. the positional projection "instock.$"
takes precedence and the $slice:1
is a no-op. instock
数组中与查询条件匹配的第一个元素(instock.$
);亦即,位置投影"instock.$"
优先,而$slice:1
是不可操作的。The "instock.$": { $slice: 1 }
does not exclude any other document field."instock.$": { $slice: 1 }
不排除任何其他文档字段。
A collection students
contains the following documents:students
集合包括以下文件:
In the following query, the projection 在下面的查询中,投影{ "grades.$": 1 }
returns only the first element greater than or equal to 85
for the grades
field.{ "grades.$": 1 }
只返回grades
字段中大于或等于85
的第一个元素。
The operation returns the following documents:该操作将返回以下文档:
Although the array field 尽管数组字段grades
may contain multiple elements that are greater than or equal to 85
, the $
projection operator returns only the first matching element from the array.grades
可能包含多个大于或等于85
的元素,但$
投影运算符仅返回数组中的第一个匹配元素。
A students
collection contains the following documents where the grades
field is an array of documents; each document contain the three field names grade
, mean
, and std
:students
集合包含以下文档,其中grades
字段是一组文档;每个文档包含三个字段名:grade
、mean
和std
:
In the following query, the projection 在下面的查询中,投影{ "grades.$": 1 }
returns only the first element with the mean
greater than 70
for the grades
field:{ "grades.$": 1 }
只返回grades
字段mean
值大于70
的第一个元素:
The operation returns the following documents:该操作将返回以下文档: