$reverseArray 연산자는 배열의 요소 순서를 반대로 하는 데 사용됩니다. 이 연산자는 배열 요소를 반대 순서로 처리하거나 표시해야 하는 경우에 유용할 수 있습니다. 배열 식 연산자이며 집계 파이프라인 내에서 사용할 수 있습니다.
문법
{
$reverseArray: <array>
}
매개 변수
| 매개 변수 | Description |
|---|---|
<array> |
역방향으로 사용할 배열입니다. |
예시
스토어 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_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
}
]
},
{
"eventName": "Discount Delight Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 5,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 5,
"Day": 18
}
}
}
],
"tag": [
"#ShopLocal",
"#FashionStore",
"#SeasonalSale",
"#FreeShipping",
"#MembershipDeals"
]
}
예제 1: 배열 순서 반전
이 쿼리는 배열 순서에 대해 반전을 수행하기 위해 연산자를 사용하는 방법을 보여 줍니다 promotionEvents .
db.stores.aggregate([
//filtering to one document
{
$match: {
_id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
}
},
{
$project: {
_id: 1,
name: 1,
promotionEventsReversed: {
$reverseArray: "$promotionEvents"
}
}
},
// Include only _id, name, promotionalDates and eventName fields in the output
{
$project: {
_id: 1,
name: 1,
"promotionEventsReversed.promotionalDates": 1,
"promotionEventsReversed.eventName": 1
}
}
])
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
"promotionEventsReversed": [
{
"eventName": "Discount Delight Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 5,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 5,
"Day": 18
}
}
},
{
"eventName": "Bargain Blitz Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 3,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 2,
"Day": 18
}
}
}
]
}
]
관련 콘텐츠
- MongoDB에서 Azure DocumentDB로 마이그레이션하기 위한 옵션을 검토합니다.
- MongoDB와의 기능 호환성에 대해 자세히 알아보세요.