$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: 문자열 비교를 사용하여 문서 일치
이 쿼리는 "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"인 문서를 _id 검색합니다.
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 Mixers에서% 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"
}
]
관련 콘텐츠
- MongoDB에서 Azure DocumentDB로 마이그레이션하기 위한 옵션을 검토합니다.
- MongoDB와의 기능 호환성에 대해 자세히 알아보세요.