本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
$replaceWith
8.0 版的新功能
Elastic 叢集不支援。
Amazon DocumentDB $replaceWith 中的彙總階段用於將輸入文件取代為新文件。輸入文件上的所有現有欄位,包括 _id 欄位,都會被新文件取代。 $replaceWith 通常用來扁平化文件,或將內嵌文件提升至頂層。
參數
-
<replacement>(必要):將取代現有文件的新文件。
範例 (MongoDB Shell)
下列範例示範如何使用 $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命令的程式碼範例,請選擇您要使用的語言標籤: