$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
  }
]