$replaceWith

$replaceWith 집계 단계 연산자는 입력 문서를 지정된 문서로 바꾸는 데 사용됩니다. $replaceWith 연산자는 문서를 한 구조에서 다른 구조로 변환하거나 완전히 새 필드와 값으로 바꿉니다.

문법

{
  "$replaceWith": <newDocument>
}

매개 변수

매개 변수 Description
newDocument 원본 문서를 바꿀 새 문서

예시

스토어 컬렉션에서 이 샘플 문서를 고려합니다.

{
    "_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 - 원본 문서의 내용을 항목의 하위 집합으로 바꾸는 문서 반환

먼저 특정 문서를 일치시켜 _id 필드로 바꾸고 문서의 내용을 지정된 필드로 바꿉니다.

db.stores.aggregate([{
    $match: {
        _id: "bda56164-954d-4f47-a230-ecf64b317b43"
    }
}, {
    $replaceWith: {
        _id: "$_id",
        name: "$name",
        sales: "$sales.totalSales"
    }
}])

이 쿼리는 다음 결과를 반환합니다.

[
  {
      "_id": "bda56164-954d-4f47-a230-ecf64b317b43",
      "name": "Boulder Innovations | Home Security Place - Ankundingburgh",
      "sales": 37015
  }
]

예제 2 - 지정된 필드를 집계한 후 원본 문서의 내용을 대체하는 문서 반환

db.stores.aggregate([{
    $match: {
        _id: "bda56164-954d-4f47-a230-ecf64b317b43"
    }
}, {
    $replaceWith: {
        _id: "$_id",
        name: "$name",
        totalStaff: {
            $add: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"]
        }
    }
}])

그러면 다음 결과가 반환됩니다.

[
  {
      "_id": "bda56164-954d-4f47-a230-ecf64b317b43",
      "name": "Boulder Innovations | Home Security Place - Ankundingburgh",
      "totalStaff": 29
  }
]