MongoDB 聚合 $match
聚合 $match
此聚合阶段的行为类似于 find。它将过滤掉不符合提供查询的文档。
尽早使用 $match
阶段可以提高性能,因为它限制了后续阶段需要处理的文档数量。
示例
In this example, we are using the "sample_airbnb" database loaded from our sample data in the Intro to Aggregations section. (在此示例中,我们使用的是在“聚合入门”部分的示例数据中加载的“sample_airbnb”数据库。)
db.listingsAndReviews.aggregate([
{ $match : { property_type : "House" } },
{ $limit: 2 },
{ $project: {
"name": 1,
"bedrooms": 1,
"price": 1
}}
])
自己动手试一试 »
这将仅返回 property_type
为“House”的文档。