国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

網(wǎng)站怎樣做友情鏈接佛山本地網(wǎng)站建設(shè)

網(wǎng)站怎樣做友情鏈接,佛山本地網(wǎng)站建設(shè),常州做網(wǎng)站哪家快,斗魚網(wǎng)站的實(shí)時視頻是怎么做的$unionWith聚合階段執(zhí)行兩個集合的合并&#xff0c;將兩個集合的管道結(jié)果合并到一個結(jié)果集傳送到下一個階段。合并后的結(jié)果文檔的順序是不確定的。 語法 { $unionWith: { coll: "<collection>", pipeline: [ <stage1>, ... ] } }要包含集合的所有文檔不…

$unionWith聚合階段執(zhí)行兩個集合的合并,將兩個集合的管道結(jié)果合并到一個結(jié)果集傳送到下一個階段。合并后的結(jié)果文檔的順序是不確定的。

語法

{ $unionWith: { coll: "<collection>", pipeline: [ <stage1>, ... ] } }

要包含集合的所有文檔不進(jìn)行任何處理,可以使用簡化形式:

{ $unionWith: "<collection>" }  // 包含指定集合的所有文檔

使用

參數(shù)字段:

字段描述
coll希望在結(jié)果集中包含的集合或視圖管道的結(jié)果
pipeline可選,應(yīng)用于coll的聚合管道[<stage1>, <stage2>, ....],聚合管道不能包含$out$merge階段。從v6.0開始,管道可以在第一個階段包含$search階段

$unionWith操作等價于下面的SQL語句:

SELECT *
FROM Collection1
WHERE ...
UNION ALL
SELECT *
FROM Collection2
WHERE ...

重復(fù)的結(jié)果

前一階段的合并結(jié)果和$unionWith階段的合并結(jié)果可能包含重復(fù)結(jié)果。例如,創(chuàng)建一個suppliers 集合和一個warehouses 集合:

db.suppliers.insertMany([{ _id: 1, supplier: "Aardvark and Sons", state: "Texas" },{ _id: 2, supplier: "Bears Run Amok.", state: "Colorado"},{ _id: 3, supplier: "Squid Mark Inc. ", state: "Rhode Island" },
])
db.warehouses.insertMany([{ _id: 1, warehouse: "A", region: "West", state: "California" },{ _id: 2, warehouse: "B", region: "Central", state: "Colorado"},{ _id: 3, warehouse: "C", region: "East", state: "Florida" },
])

下面的聚合合并了聚合supplierswarehouse的state’字段投影結(jié)果。

db.suppliers.aggregate([{ $project: { state: 1, _id: 0 } },{ $unionWith: { coll: "warehouses", pipeline: [ { $project: { state: 1, _id: 0 } } ]} }
])

結(jié)果包含重復(fù)的文檔

{ "state" : "Texas" }
{ "state" : "Colorado" }
{ "state" : "Rhode Island" }
{ "state" : "California" }
{ "state" : "Colorado" }
{ "state" : "Florida" }

要要去除重復(fù),可以加個$group階段,按照state字段分組:

db.suppliers.aggregate([{ $project: { state: 1, _id: 0 } },{ $unionWith: { coll: "warehouses", pipeline: [ { $project: { state: 1, _id: 0 } } ]} },{ $group: { _id: "$state" } }
])

這樣結(jié)果就沒有重復(fù)的文檔了:

 { "_id" : "California" }{ "_id" : "Texas" }{ "_id" : "Florida" }{ "_id" : "Colorado" }{ "_id" : "Rhode Island" }

$unionWith 分片集合

如果$unionWith階段是$lookup管道的一部分,則$unionWithcoll被分片。例如,在下面的聚合操作中,不能對inventory_q1集合進(jìn)行分片:

db.suppliers.aggregate([{$lookup: {from: "warehouses",let: { order_item: "$item", order_qty: "$ordered" },pipeline: [...{ $unionWith: { coll: "inventory_q1", pipeline: [ ... ] } },...],as: "stockdata"}}
])

限制

  • 聚合管道不能在事務(wù)中使用$unionWith
  • 如果$unionWith階段是$lookup管道的一部分,則$unionWith的集合不能被分片
  • unionWith管道不能包含out階段
  • unioWith管道不能包含merge階段

舉例

用多年的數(shù)據(jù)集合創(chuàng)建銷售報告

下面示例使用$unionWith階段來合并數(shù)據(jù)并返回多個集合的結(jié)果,在這些示例中,每個集合都包含一年的銷售數(shù)據(jù)。

填充樣本數(shù)據(jù):

分別創(chuàng)建sales_2017、sales_2018、sales_2019、sales_2019四個集合并填充數(shù)據(jù):

db.sales_2017.insertMany( [{ store: "General Store", item: "Chocolates", quantity: 150 },{ store: "ShopMart", item: "Chocolates", quantity: 50 },{ store: "General Store", item: "Cookies", quantity: 100 },{ store: "ShopMart", item: "Cookies", quantity: 120 },{ store: "General Store", item: "Pie", quantity: 10 },{ store: "ShopMart", item: "Pie", quantity: 5 }
] )
db.sales_2018.insertMany( [{ store: "General Store", item: "Cheese", quantity: 30 },{ store: "ShopMart", item: "Cheese", quantity: 50 },{ store: "General Store", item: "Chocolates", quantity: 125 },{ store: "ShopMart", item: "Chocolates", quantity: 150 },{ store: "General Store", item: "Cookies", quantity: 200 },{ store: "ShopMart", item: "Cookies", quantity: 100 },{ store: "ShopMart", item: "Nuts", quantity: 100 },{ store: "General Store", item: "Pie", quantity: 30 },{ store: "ShopMart", item: "Pie", quantity: 25 }
] )
db.sales_2019.insertMany( [{ store: "General Store", item: "Cheese", quantity: 50 },{ store: "ShopMart", item: "Cheese", quantity: 20 },{ store: "General Store", item: "Chocolates", quantity: 125 },{ store: "ShopMart", item: "Chocolates", quantity: 150 },{ store: "General Store", item: "Cookies", quantity: 200 },{ store: "ShopMart", item: "Cookies", quantity: 100 },{ store: "General Store", item: "Nuts", quantity: 80 },{ store: "ShopMart", item: "Nuts", quantity: 30 },{ store: "General Store", item: "Pie", quantity: 50 },{ store: "ShopMart", item: "Pie", quantity: 75 }
] )
db.sales_2020.insertMany( [{ store: "General Store", item: "Cheese", quantity: 100, },{ store: "ShopMart", item: "Cheese", quantity: 100},{ store: "General Store", item: "Chocolates", quantity: 200 },{ store: "ShopMart", item: "Chocolates", quantity: 300 },{ store: "General Store", item: "Cookies", quantity: 500 },{ store: "ShopMart", item: "Cookies", quantity: 400 },{ store: "General Store", item: "Nuts", quantity: 100 },{ store: "ShopMart", item: "Nuts", quantity: 200 },{ store: "General Store", item: "Pie", quantity: 100 },{ store: "ShopMart", item: "Pie", quantity: 100 }
] )

按照year、store和item的銷售額

db.sales_2017.aggregate( [{ $set: { _id: "2017" } },{ $unionWith: { coll: "sales_2018", pipeline: [ { $set: { _id: "2018" } } ] } },{ $unionWith: { coll: "sales_2019", pipeline: [ { $set: { _id: "2019" } } ] } },{ $unionWith: { coll: "sales_2020", pipeline: [ { $set: { _id: "2020" } } ] } },{ $sort: { _id: 1, store: 1, item: 1 } }
] )
  • $set階段用于更新_id字段,使其包含年份
  • $unionWith階段,將四個集合中的所有文檔合并在一起,每個文檔也使用$set階段
  • $sort階段,按照_id(年份)、storeitem進(jìn)行排序

管道輸出:

{ "_id" : "2017", "store" : "General Store", "item" : "Chocolates", "quantity" : 150 }
{ "_id" : "2017", "store" : "General Store", "item" : "Cookies", "quantity" : 100 }
{ "_id" : "2017", "store" : "General Store", "item" : "Pie", "quantity" : 10 }
{ "_id" : "2017", "store" : "ShopMart", "item" : "Chocolates", "quantity" : 50 }
{ "_id" : "2017", "store" : "ShopMart", "item" : "Cookies", "quantity" : 120 }
{ "_id" : "2017", "store" : "ShopMart", "item" : "Pie", "quantity" : 5 }
{ "_id" : "2018", "store" : "General Store", "item" : "Cheese", "quantity" : 30 }
{ "_id" : "2018", "store" : "General Store", "item" : "Chocolates", "quantity" : 125 }
{ "_id" : "2018", "store" : "General Store", "item" : "Cookies", "quantity" : 200 }
{ "_id" : "2018", "store" : "General Store", "item" : "Pie", "quantity" : 30 }
{ "_id" : "2018", "store" : "ShopMart", "item" : "Cheese", "quantity" : 50 }
{ "_id" : "2018", "store" : "ShopMart", "item" : "Chocolates", "quantity" : 150 }
{ "_id" : "2018", "store" : "ShopMart", "item" : "Cookies", "quantity" : 100 }
{ "_id" : "2018", "store" : "ShopMart", "item" : "Nuts", "quantity" : 100 }
{ "_id" : "2018", "store" : "ShopMart", "item" : "Pie", "quantity" : 25 }
{ "_id" : "2019", "store" : "General Store", "item" : "Cheese", "quantity" : 50 }
{ "_id" : "2019", "store" : "General Store", "item" : "Chocolates", "quantity" : 125 }
{ "_id" : "2019", "store" : "General Store", "item" : "Cookies", "quantity" : 200 }
{ "_id" : "2019", "store" : "General Store", "item" : "Nuts", "quantity" : 80 }
{ "_id" : "2019", "store" : "General Store", "item" : "Pie", "quantity" : 50 }
{ "_id" : "2019", "store" : "ShopMart", "item" : "Cheese", "quantity" : 20 }
{ "_id" : "2019", "store" : "ShopMart", "item" : "Chocolates", "quantity" : 150 }
{ "_id" : "2019", "store" : "ShopMart", "item" : "Cookies", "quantity" : 100 }
{ "_id" : "2019", "store" : "ShopMart", "item" : "Nuts", "quantity" : 30 }
{ "_id" : "2019", "store" : "ShopMart", "item" : "Pie", "quantity" : 75 }
{ "_id" : "2020", "store" : "General Store", "item" : "Cheese", "quantity" : 100 }
{ "_id" : "2020", "store" : "General Store", "item" : "Chocolates", "quantity" : 200 }
{ "_id" : "2020", "store" : "General Store", "item" : "Cookies", "quantity" : 500 }
{ "_id" : "2020", "store" : "General Store", "item" : "Nuts", "quantity" : 100 }
{ "_id" : "2020", "store" : "General Store", "item" : "Pie", "quantity" : 100 }
{ "_id" : "2020", "store" : "ShopMart", "item" : "Cheese", "quantity" : 100 }
{ "_id" : "2020", "store" : "ShopMart", "item" : "Chocolates", "quantity" : 300 }
{ "_id" : "2020", "store" : "ShopMart", "item" : "Cookies", "quantity" : 400 }
{ "_id" : "2020", "store" : "ShopMart", "item" : "Nuts", "quantity" : 200 }
{ "_id" : "2020", "store" : "ShopMart", "item" : "Pie", "quantity" : 100 }

根據(jù)item匯總銷售額

db.sales_2017.aggregate( [{ $unionWith: "sales_2018" },{ $unionWith: "sales_2019" },{ $unionWith: "sales_2020" },{ $group: { _id: "$item", total: { $sum: "$quantity" } } },{ $sort: { total: -1 } }
] )
  • $unionWith階段將文檔從指定的集合檢索到管道中
  • $group階段按item字段分組,并使用$sum計算每個item的總銷售量
  • $sort階段按合計降序排列文檔

結(jié)果:

{ "_id" : "Cookies", "total" : 1720 }
{ "_id" : "Chocolates", "total" : 1250 }
{ "_id" : "Nuts", "total" : 510 }
{ "_id" : "Pie", "total" : 395 }
{ "_id" : "Cheese", "total" : 350 }
http://aloenet.com.cn/news/30509.html

相關(guān)文章:

  • 網(wǎng)站開發(fā)看書湖北網(wǎng)站seo
  • 做動漫網(wǎng)站侵權(quán)嗎揚(yáng)州網(wǎng)絡(luò)優(yōu)化推廣
  • 怎么通過微博做網(wǎng)站外鏈百度seo外包
  • wordpress游戲網(wǎng)站百度優(yōu)化推廣
  • 做網(wǎng)站賭博的推廣是不是犯罪的上海全網(wǎng)推廣
  • 公司網(wǎng)站 域名網(wǎng)絡(luò)營銷成功案例有哪些
  • 商業(yè)網(wǎng)站建設(shè)的方法外國網(wǎng)站怎么進(jìn)入
  • 做圖素材網(wǎng)站開哪個vip好熱搜榜上2023年熱門話題
  • 怎么在外管局的網(wǎng)站做延期seo服務(wù)價格表
  • 怎樣做_網(wǎng)站做seo百度網(wǎng)站的域名地址
  • 3d做號網(wǎng)站每日精選12條新聞
  • 設(shè)計前沿的網(wǎng)站東莞網(wǎng)站關(guān)鍵詞優(yōu)化公司
  • 做社群的網(wǎng)站有哪些西安百度推廣怎么做
  • wordpress html插件優(yōu)化網(wǎng)站做什么的
  • 哪家公司做網(wǎng)站最好網(wǎng)絡(luò)營銷專業(yè)技能
  • 明年做那些網(wǎng)站致富網(wǎng)站優(yōu)化包括哪些
  • 無極網(wǎng)站站怎么有的下不了如何讓百度收錄自己信息
  • 新都網(wǎng)站開發(fā)鄭州百度網(wǎng)站優(yōu)化排名
  • 營銷網(wǎng)站的搭建磁力兔子
  • 類似非小號的網(wǎng)站怎么做軟文推廣一般發(fā)布在哪些平臺
  • 上城網(wǎng)站建設(shè)百度快照推廣是什么意思
  • 網(wǎng)站添加視頻代碼網(wǎng)絡(luò)建站流程
  • 亞馬遜做deal的網(wǎng)站淘寶指數(shù)網(wǎng)址
  • 網(wǎng)站建設(shè)怎樣上傳程序企業(yè)網(wǎng)站營銷的實(shí)現(xiàn)方式
  • 做網(wǎng)站的畢設(shè)開題依據(jù)在線注冊網(wǎng)站
  • 如何在office做網(wǎng)站360站長平臺
  • 公司網(wǎng)站維護(hù)好做嗎百度移動端排名軟件
  • 網(wǎng)站建設(shè)外包流程網(wǎng)站排名優(yōu)化怎樣做
  • 萊蕪亓家網(wǎng)站優(yōu)化神馬網(wǎng)站關(guān)鍵詞排名價格
  • 網(wǎng)站建設(shè)在線視頻百度云搜索入口