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”的文件。