“文档” 菜单
文档首页
/
MongoDB Manual
/ / / /

$elemMatch(查询)

在此页面上

  • 定义
  • 兼容性
  • 语法
  • 行为
  • 举例
  • 了解详情

提示

另请参阅:

$elemMatch(投影)

$elemMatch

$elemMatch 操作符匹配包含数组字段的文档,其中至少有一个元素匹配所有指定查询条件。

可以使用 $elemMatch 查找托管在以下环境中的部署:

  • MongoDB Atlas :用于在云中部署 MongoDB 的完全托管服务

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

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 } }
] )

_id5 的文档不包含数组。包含该文档以表明 $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时的输出差异。

使用$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" }
)

在以下输出中,请注意还包括 _id5 的文档(不包含数组):

[
{
_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 } }
]

考虑以下查询:

  • 第一个查询在 $elemMatch 中只有一个 <query> 条件。

  • 第二个查询省略了$elemMatch

第一个使用$elemMatch的查询:

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

此查询返回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 } ] }
{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "def", "score" : 8 } ] }

不带$elemMatch第二个查询:

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

此查询返回product中的任何一个results都不为"xyz"的文档:

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

这两个查询均包含 _id4 的文档,并省略 _id5 的文档,因为 product"xyz"

有关查询数组的其他示例,请参阅:

有关查询的其他示例,请参阅:

提示

另请参阅:

← $all
$size →