本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
$replaceWith
8.0 版的新增内容
弹性集群不支持。
Amazon DocumentDB 中的$replaceWith聚合阶段用于将输入文档替换为新文档。输入文档上的所有现有字段(包括 _id 字段)都将替换为新文档。 $replaceWith通常用于扁平化文档或将嵌入式文档提升到顶层。
参数
-
<replacement>(必填):将取代现有文档的新文档。
示例(MongoDB 外壳)
以下示例演示如何使用$replaceWith运算符替换 Amazon DocumentDB 集合中的现有文档。
创建示例文档
db.restaurants.insertMany([ { "restaurantId": "REST-0Y9GL0", "name": "Biryani Adda", "cuisine": "Indian", "ratings": [ 3, 4, 3, 2, 2, 4, 1, 5, 5, 5 ] }, { "restaurantId": "REST-8L2PX9", "name": "The Burger Spot", "cuisine": "American", "ratings": [ 2, 3, 4, 5, 3, 1, 1, 2, 4 ] } ]);
查询示例
db.restaurants.aggregate([ { $replaceWith: { name: "$name", cuisine: "$cuisine", rating: { $avg: "$ratings" } } } ]);
输出
[
{
name: 'Biryani Adda',
cuisine: 'Indian',
rating: 3.4
},
{
name: 'The Burger Spot',
cuisine: 'American',
rating: 2.7777777777777777
}
]
代码示例
要查看使用该$replaceWith命令的代码示例,请选择要使用的语言的选项卡: