$elemMatch(查询)
定义
$elemMatch
这
$elemMatch
运算符匹配包含至少一个元素符合所有指定查询条件的数组字段的文档。
兼容性
您可以使用 $elemMatch
在以下环境中部署
MongoDB Atlas:云中MongoDB部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的MongoDB版本
MongoDB Community:源代码可用的、免费使用并可自我管理的MongoDB版本
语法
{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }
行为
您不能在
$where
操作符中指定$elemMatch
.您不能在
$text
查询操作符中指定$elemMatch
.
示例
元素匹配
以下是在 scores
集合中的文档
{ _id: 1, results: [ 82, 85, 88 ] } { _id: 2, results: [ 75, 88, 89 ] }
以下查询仅匹配那些 results
数组中至少有一个元素既大于或等于 80
又小于 85
的文档
db.scores.find( { results: { $elemMatch: { $gte: 80, $lt: 85 } } } )
查询返回以下文档,因为元素 82
既大于或等于 80
又小于 85
{ "_id" : 1, "results" : [ 82, 85, 88 ] }
有关在数组元素上指定多个条件的更多信息,请参阅指定数组元素的多个条件。
嵌入文档数组
此语句将文档插入到 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 } ] }, { "_id": 5, "results": { "product": "xyz", "score": 7 } } ] )
具有 _id
为 5
的文档不包含数组。该文档被包含进来,以显示 $elemMatch
只匹配数组元素,您将在以下示例中看到。
以下查询匹配文档,其中 results
包含至少一个元素,其中 product
是 "xyz"
且 score
大于或等于 8
db.survey.find( { results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } } )
具体来说,查询匹配以下文档
{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "xyz", "score" : 8 } ] }
单一查询条件
以下部分显示了使用 $elemMatch
与单一查询条件,并省略 $elemMatch
时的输出差异。
示例 1
带有 $elemMatch
的查询
db.survey.find( { results: { $elemMatch: { product: "xyz" } } } )
该查询返回任何 results
中的 product
是 "xyz"
的文档
[ { _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 } ] } ]
没有 $elemMatch
的查询
db.survey.find( { "results.product": "xyz" } )
在以下输出中,请注意具有 _id
为 5
(不包含数组)的文档也被包含在内
[ { _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: 5, results: { product: 'xyz', score: 7 } } ]
示例 2
考虑以下查询
第一个查询在
$elemMatch
中有一个单个的<query>
条件。第二个查询省略了
$elemMatch
。
第一个查询包含$elemMatch
db.survey.find( { "results": { $elemMatch: { product: { $ne: "xyz" } } } } )
查询返回具有不同于"xyz"
的product
值的文档。
{ "_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 } ] }
第二个查询没有$elemMatch
。
db.survey.find( { "results.product": { $ne: "xyz" } } )
查询返回没有product
结果为"xyz"
的文档。
{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "def", "score" : 8 } ] }
两个查询都包括_id
为4
的文档,并省略了_id
为5
的文档,因为其product
为"xyz"
。
了解更多
有关查询数组的更多示例,请参阅
有关查询的更多示例,请参阅查询文档。