See also参阅
On this page本页内容
$elemMatch
¶The $elemMatch
operator limits the contents of an <array>
field from the query results to contain only the first element matching the $elemMatch
condition.$elemMatch
运算符将查询结果中<array>
字段的内容限制为仅包含与$elemMatch
条件匹配的第一个元素。
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.有关示例,请参阅数组字段限制。
Starting in MongoDB 4.4, regardless of the ordering of the fields in the document, the 从MongoDB 4.4开始,无论文档中字段的顺序如何,现有字段的$elemMatch
projection of an existing field returns the field after the other existing field inclusions.$elemMatch
投影都会在其他现有字段包含之后返回该字段。
For example, consider a 例如,考虑一个具有以下文档的players
collection with the following document:players
集合:
In version 4.4+, the following projection returns the 在版本4.4+中,以下投影将在投影中包含的其他现有字段之后返回games
field after the other existing fields included in the projection even though in the document, the field is listed before joined
and lastLogin
fields:games
字段,即使在文档中,该字段在joined
和lastLogin
字段之前列出:
That is, the operation returns the following document:也就是说,该操作返回以下文档:
In version 4.2 and earlier, the 在4.2版及更早版本中,现有字段的$elemMatch
projection of an existing field upholds the ordering in the document:$elemMatch
投影支持文档中的顺序:
db.collection.find()
operations on views do not support $elemMatch
projection operator.db.collection.find()
操作不支持$elemMatch
投影运算符。$text
query expression in an $elemMatch
.$elemMatch
中指定$text
查询表达式。The examples on the $elemMatch
projection operator assumes a collection schools
with the following documents:$elemMatch
投影运算符上的示例假定集合schools
包含以下文档:
The following find()
operation queries for all documents where the value of the zipcode
field is 63109
. The $elemMatch
projection returns only the first matching element of the students
array where the school
field has a value of 102
:
The operation returns the following documents that have zipcode
equal to 63109
and projects the students
array using $elemMatch
:
_id
equal to 1
, the students
array contains multiple elements with the school
field equal to 102
. However, the $elemMatch
projection returns only the first matching element from the array._id
equal to 3
does not contain the students
field in the result since no element in its students
array matched the $elemMatch
condition.$elemMatch
with Multiple Fields¶The $elemMatch
projection can specify criteria on multiple fields:$elemMatch
投影可以在多个字段上指定条件:
The following 下面的find()
operation queries for all documents where the value of the zipcode
field is 63109
. find()
操作查询zipcode
字段值为63109
的所有文档。The projection includes the first matching element of the 投影包括students
array where the school
field has a value of 102
and the age
field is greater than 10
:students
数组的first
匹配元素,其中school
字段的值为102
,age
字段的值大于10
:
The operation returns the three documents that have 该操作返回zipcode
equal to 63109
:zipcode
等于63109
的三个文档:
The document with _id
equal to 3
does not contain the students
field since no array element matched the $elemMatch
criteria._id
等于3
的文档不包含students
字段,因为没有与$elemMatch
条件匹配的数组元素。
See also参阅
$ (projection)
operator