On this page本页内容
See also参阅
$elemMatch
¶The $elemMatch
operator matches documents that contain an array field with at least one element that matches all the specified query criteria.$elemMatch
运算符将包含数组字段的文档与至少一个匹配所有指定查询条件的元素相匹配。
If you specify only a single 如果仅在<query>
condition in the $elemMatch
expression, and are not using the $not
or $ne
operators inside of $elemMatch
, $elemMatch
can be omitted. $elemMatch
表达式内指定一个<query>
条件,并且未使用$not
或$ne
运算符,则$elemMatch
可以省略。See Single Query Condition.请参阅单个查询条件。
$where
expression in an $elemMatch
.$elemMatch
中指定$where
表达式。$text
query expression in an $elemMatch
.$elemMatch
中指定$text
查询表达式。Given the following documents in the 考虑到scores
collection:scores
集合中的以下文档:
The following query matches only those documents where the 以下查询仅匹配results
array contains at least one element that is both greater than or equal to 80
and is less than 85
:results
数组中至少包含一个大于或等于80
且小于85
的元素的文档:
The query returns the following document since the element 由于元素82
is both greater than or equal to 80
and is less than 85
:82
大于或等于80
且小于85
,因此查询返回以下文档:
For more information on specifying multiple criteria on array elements, see Specify Multiple Conditions for Array Elements.有关在数组元素上指定多个条件的详细信息,请参阅为数组元素指定多个条件。
This statement inserts documents into the 此语句将文档插入到survey
collection:survey
集合中:
The following query matches only those documents where the 以下查询仅匹配results
array contains at least one element with both product
equal to "xyz"
and score
greater than or equal to 8
:results
数组至少包含一个元素其product
等于"xyz"
且score
大于或等于8
的文档:
Specifically, the query matches the following document:具体而言,查询与以下文档匹配:
If you specify a single query predicate in the 如果在$elemMatch
expression, and are not using the $not
or $ne
operators inside of $elemMatch
, $elemMatch
can be omitted.$elemMatch
表达式中指定单个查询谓词,并且未在$elemMatch
中使用$not
或$ne
运算符,则可以省略$elemMatch
。
The following examples return the same documents.以下示例返回相同的文档。
With 使用$elemMatch
:$elemMatch
:
Without <不使用$elemMatch
:$elemMatch
:
However, if your 但是,如果$elemMatch
expression contains the $not
or $ne
operators then omitting the $elemMatch
expression changes the documents returned.$elemMatch
表达式包含$not
或$ne
运算符,则省略$elemMatch
表达式会更改返回的文档。
The following examples return different documents.以下示例返回不同的文档。
With 使用$elemMatch
:$elemMatch
:
Without 不使用$elemMatch
:$elemMatch
:
With 使用$elemMatch
, the first query returns these documents:$elemMatch
,第一个查询将返回以下文档:
Without 不使用$elemMatch
, the second query returns this document:$elemMatch
,第二个查询将返回此文档:
The first query returns the documents where any product in the 第一个查询返回results
array is not "xyz"
. results
数组中任一个product
不是"xyz"
的文档。The second query returns the documents where all of the products in the 第二个查询返回results
array are not "xyz"
."xyz"
数组中的所有product
都不是"xyz"
的文档。
For additional examples in querying arrays, see:有关查询数组的其他示例,请参阅:
For additional examples in querying, see:有关查询中的其他示例,请参阅:
See also参阅