$elemMatch 연산자는 지정된 모든 쿼리 기준과 일치하는 요소가 하나 이상 있는 배열 필드가 포함된 문서를 일치시키는 데 사용됩니다. 이 연산자는 지정된 요소가 있는 배열 문서를 찾아야 하는 경우에 유용합니다.
문법
db.collection.find({ <field>: { $elemMatch: { <query1>, <query2>, ... } } })
매개 변수
| 매개 변수 | Description |
|---|---|
field |
쿼리할 배열이 포함된 문서의 필드. |
query |
배열의 적어도 하나의 요소가 충족해야 하는 조건. |
예시
스토어 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
예 1: 요소 목록 중 특정 요소를 배열에서 찾기
이 쿼리는 컬렉션에서 배열에서 stores 범주 이름 "DJ 조명"을 사용하여 하나 이상의 할인이 있는 처음 두 문서를 검색합니다 promotionEvents . 해당 쿼리는 해당 문서에 대한 _id 및 promotionEvents.discounts 필드만 반환합니다.
db.stores.find({
"promotionEvents.discounts": {
$elemMatch: {
categoryName: "DJ Lighting"
}
}
}, {
_id: 1,
"promotionEvents.discounts": 1
}).limit(2)
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"promotionEvents": [
{
"discounts": [
{
"categoryName": "DJ Turntables",
"discountPercentage": 18
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 15
}
]
},
{
"discounts": [
{
"categoryName": "DJ Lighting",
"discountPercentage": 14
},
{
"categoryName": "DJ Cases",
"discountPercentage": 20
}
]
}
]
},
{
"_id": "91de5201-8194-44bf-848f-674e8df8bf5e",
"promotionEvents": [
{
"discounts": [
{
"categoryName": "DJ Cases",
"discountPercentage": 6
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 14
}
]
},
{
"discounts": [
{
"categoryName": "DJ Headphones",
"discountPercentage": 19
},
{
"categoryName": "DJ Speakers",
"discountPercentage": 13
}
]
},
{
"discounts": [
{
"categoryName": "DJ Lighting",
"discountPercentage": 12
},
{
"categoryName": "DJ Accessories",
"discountPercentage": 6
}
]
}
]
}
]
관련 콘텐츠
- MongoDB에서 Azure DocumentDB로 마이그레이션하기 위한 옵션을 검토합니다.
- MongoDB와의 기능 호환성에 대해 자세히 알아보세요.