共用方式為


$match

$match匯總管線中的階段可用來篩選符合指定條件的檔。 這與作業類似, find 但在匯總管線內用來縮小傳遞至下一個階段的檔範圍。 此階段可藉由減少後續階段中需要處理的檔數目,來優化效能。

語法

{
  $match: {
    <query>
  }
}

參數

參數 Description
<query> 標準 MongoDB 查詢檔,指定文件必須符合的條件。

範例

請參考商店集合中的此範例檔。

{
  "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
   "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
  "location": {
    "lat": 60.1441,
    "lon": -141.5012
  },
  "staff": {
    "totalStaff": {
      "fullTime": 2,
      "partTime": 0
    }
  },
  "sales": {
    "salesByCategory": [
      {
        "categoryName": "DJ Headphones",
        "totalSales": 35921
      }
    ],
    "fullSales": 3700
  },
  "promotionEvents": [
    {
      "eventName": "Bargain Blitz Days",
      "promotionalDates": {
        "startDate": {
          "Year": 2024,
          "Month": 3,
          "Day": 11
        },
        "endDate": {
          "Year": 2024,
          "Month": 2,
          "Day": 18
        }
      },
      "discounts": [
        {
          "categoryName": "DJ Turntables",
          "discountPercentage": 18
        },
        {
          "categoryName": "DJ Mixers",
          "discountPercentage": 15
        }
      ]
    }
  ],
  "tag": [
    "#ShopLocal",
    "#SeasonalSale",
    "#FreeShipping",
    "#MembershipDeals"
  ]
}

範例 1:使用字串比較比對檔

此查詢會擷取 「 _id 7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5」 的文件:

db.stores.aggregate([
    {
      $match: {
        "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
      }
    }
])

此查詢會傳回下列結果:

[
    {
        "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
        "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
        "location": {
            "lat": 60.1441,
            "lon": -141.5012
        },
        "staff": {
            "employeeCount": {
                "fullTime": 2,
                "partTime": 0
            }
        },
        "sales": {
            "salesByCategory": [
                {
                    "categoryName": "DJ Headphones",
                    "totalSales": 35921
                },
                {
                    "categoryName": "DJ Cables",
                    "totalSales": 1000
                }
            ],
            "fullSales": 3700
        },
        "promotionEvents": [],
        "tag": [
            "#ShopLocal",
            "#NewArrival",
            "#NewArrival",
            "#FreeShipping"
        ],
        "company": "Lakeshore Retail",
        "city": "Port Cecile",
        "lastUpdated": "2025-08-04T05:57:04.619Z",
        "storeOpeningDate": "2024-09-12T10:21:58.274Z"
    }
]

範例 2:使用數值比較比對檔

此查詢會擷取總銷售額大於 $35,000 的所有商店:

db.stores.aggregate([
    {
      $match: {
        "sales.totalSales": { $gt: 35000 }
      }
    },
    // Limit the result to the first 3 documents
    { $limit: 3 },
      // Include only _id and name fields in the output 
    { $project: { _id: 1, name: 1 } } 
])

此查詢傳回的前三個結果為:

[
  {
    "_id": "8345de34-73ec-4a99-9cb6-a81f7b145c34",
    "name": "Northwind Traders | Bed and Bath Place - West Oraland"
  },
  {
    "_id": "57cc4095-77d9-4345-af20-f8ead9ef0197",
    "name": "Wide World Importers | Bed and Bath Store - West Vitafort"
  },
  {
    "_id": "560099f8-325f-4c35-a4e5-2e0879eb95af",
    "name": "Wide World Importers | Bed and Bath Depot - North Maritzaberg"
  }
]

範例 3:比對子檔中的檔

此查詢會擷取所有在DJ混音器上享有15% 折扣的商店:

db.stores.aggregate([
    {
      $match: {
        "promotionEvents.discounts": {
          $elemMatch: {
            "categoryName": "DJ Mixers",
            "discountPercentage": 15
          }
        }
      }
    },
    // Limit the result to the first 3 documents
    { $limit: 3 },
      // Include only _id and name fields in the output 
    { $project: { _id: 1, name: 1 } } 
])

此查詢傳回的前三個結果為:

[
  {
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile"
  },
  {
    "_id": "3c7eda41-23a1-4226-abf6-17ee9e851b5b",
    "name": "Boulder Innovations | DJ Equipment Bazaar - New Ceasarview"
  },
  {
    "_id": "63831a7d-13a9-4d8b-bf1d-ac004057f96d",
    "name": "Contoso, Ltd. | DJ Equipment Shop - Reillyfurt"
  }
]