MongoDB 聚合 $out
聚合 $out
此聚合階段將聚合管道返回的文件寫入一個集合。
The $out
stage must be the last stage of the aggregation pipeline. ($out 階段必須是聚合管道的最後一個階段。)
示例
In this example, we are using the "sample_airbnb" database loaded from our sample data in the Intro to Aggregations section. (在此示例中,我們使用的是在“聚合入門”部分的示例資料中載入的“sample_airbnb”資料庫。)
db.listingsAndReviews.aggregate([
{
$group: {
_id: "$property_type",
properties: {
$push: {
name: "$name",
accommodates: "$accommodates",
price: "$price",
},
},
},
},
{ $out: "properties_by_type" },
])
自己動手試一試 »
The first stage will group properties by the property_type
and include the name
, accommodates
, and price
fields for each. The $out
stage will create a new collection called properties_by_type
in the current database and write the resulting documents into that collection. (第一個階段將按 property_type
對房產進行分組,併為每個房產包含 name
、accommodates
和 price
欄位。$out 階段將在當前資料庫中建立一個名為 properties_by_type
的新集合,並將結果文件寫入該集合。)