$elemMatch (query)

On this page本页内容

Tip提示
See also: 参阅:

$elemMatch (projection)(投影)

Definition定义

$elemMatch

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.$elemMatch运算符将包含数组字段的文档与至少一个匹配所有指定查询条件的元素相匹配。

{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }

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>条件,并且没有在$elem Match中使用$not$ne运算符,则可以省略$elemMatchSee Single Query Condition.请参见单个查询条件

Behavior行为

  • You cannot specify a $where expression in an $elemMatch.不能在$elemMatch中指定$where表达式。
  • You cannot specify a $text query expression in an $elemMatch.不能在$elemMatch中指定$text查询表达式。

Examples示例

Element Match元素匹配

Given the following documents in the scores collection:给出scores集合中的以下文件:

{ _id: 1, results: [ 82, 85, 88 ] }
{ _id: 2, results: [ 75, 88, 89 ] }

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的元素的文档:

db.scores.find(
   { results: { $elemMatch: { $gte: 80, $lt: 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,因此查询返回以下文档:

{ "_id" : 1, "results" : [ 82, 85, 88 ] }

For more information on specifying multiple criteria on array elements, see Specify Multiple Conditions for Array Elements.有关在数组元素上指定多个条件的详细信息,请参阅为数组元素指定多种条件

Array of Embedded Documents嵌入式文档数组

This statement inserts documents into the survey collection:此语句将文档插入survey集合:

db.survey.insertMany( [
   { "_id": 1, "results": [ { "product": "abc", "score": 10 },
                            { "product": "xyz", "score": 5 } ] },
   { "_id": 2, "results": [ { "product": "abc", "score": 8 },
                            { "product": "xyz", "score": 7 } ] },
   { "_id": 3, "results": [ { "product": "abc", "score": 7 },
                            { "product": "xyz", "score": 8 } ] },
   { "_id": 4, "results": [ { "product": "abc", "score": 7 },
                            { "product": "def", "score": 8 } ] }
] )

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的文档:

db.survey.find(
   { results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } }
)

Specifically, the query matches the following document:具体来说,查询匹配以下文档:

{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 },
                           { "product" : "xyz", "score" : 8 } ] }

Single Query Condition单个查询条件

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表达式中指定了单个查询谓词,并且没有在$elem匹配中使用$not$ne运算符,则可以省略$elemMatch

The following examples return the same documents.以下示例返回相同的文档。

With $elemMatch:使用$elemMatch

db.survey.find(
   { results: { $elemMatch: { product: "xyz" } } }
)

Without $elemMatch:没使用$elemMatch

db.survey.find(
   { "results.product": "xyz" }
)

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

db.survey.find(
   { "results": { $elemMatch: { product: { $ne: "xyz" } } } }
)

Without $elemMatch:没使用$elemMatch

db.survey.find(
   { "results.product": { $ne: "xyz" } }
)

With $elemMatch, the first query returns these documents:使用$elemMatch,第一个查询将返回以下文档:

{ "_id" : 1, "results" : [ { "product" : "abc", "score" : 10 },
                           { "product" : "xyz", "score" : 5 } ] }
{ "_id" : 2, "results" : [ { "product" : "abc", "score" : 8 },
                           { "product" : "xyz", "score" : 7 } ] }
{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 },
                           { "product" : "xyz", "score" : 8 } ] }
{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 },
                           { "product" : "def", "score" : 8 } ] }

Without $elemMatch, the second query returns this document:如果没有$elemMatch,第二个查询将返回此文档:

{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 },
                           { "product" : "def", "score" : 8 } ] }

The first query returns the documents where any product in the results array is not "xyz". 第一个查询返回results数组中任一个产品不是"xyz"的文档。The second query returns the documents where all of the products in the results array are not "xyz".第二个查询返回results数组中所有产品都不是"xyz"的文档。

Additional Examples更多示例

For additional examples in querying arrays, see:有关查询数组的其他示例,请参阅:

For additional examples in querying, see:有关查询的其他示例,请参阅:

Tip提示
See also: 参阅:
←  $all$size →