[英]MongoDB Aggregation Sum Growth
我有一個MondoDB聚合管道,通過匯總所有Sales
來輸出每日收入:
Sale.aggregate
([
{
$project: {
day: { $substr: ['$createdAt', 0, 10], },
},
},
{
$group: {
_id: '$day',
earnings: { $sum: '$pricing.earnings' },
},
},
{
$sort: {
_id: 1
}
},
{
$project: {
date: '$_id',
earnings: '$earnings',
},
},
{
$group: {
_id: null,
stats: { $push: '$$ROOT' },
}
},
{
$project: {
stats: {
$map: {
// returns an array with all dates between 2 provided params
input: dates.daysArray(dates.getDay(user.createdAt), dates.today()),
as: 'date',
in: {
$let: {
vars: { index: { $indexOfArray: ['$stats._id', '$$date'] } },
in: {
$cond: {
if: { $ne: ['$$index', -1] },
then: { $arrayElemAt: ['$stats', '$$index'] },
else: { _id: '$$date', date: '$$date', earnings: 0 }
}
}
}
}
}
}
}
},
{
$unwind: '$stats'
},
{
$replaceRoot: {
newRoot: '$stats'
}
},
{
$project: {
_id: 0,
x: '$date',
y: '$earnings',
},
},
])
該管道的輸出可能如下所示:
[
{
x: '2019-01-09',
y: 10,
},
{
x: '2019-01-10',
y: 5,
},
{
x: '2019-01-11',
y: 20,
}
]
這意味着在2019-01-09
有10美元的銷售額,在2019-01-10
5美元的銷售額, 2019-01-10
。
現在,我不想要每日收入,而是想要獲得的所有收入的總和。 這意味着結果應如下所示:
[
{
x: '2019-01-09',
y: 10, // a total of $10 on the first day
},
{
x: '2019-01-10',
y: 15, // a total of $15 on the second day (10 + 5)
},
{
x: '2019-01-11',
y: 35, // a total of $35 on the third day (15 + 20)
}
]
所以基本上我不想要每日數量,而是增長。
PS:我正在使用此數據顯示在圖表中。
您可以在擁有的基礎上添加以下階段
邏輯是$push
$arrayElemAt
$push
x和y $push
到數組,使用$range
按索引迭代數組,對於x在index
處獲取$arrayElemAt
,對於y $slice
和$sum
直到index+1
db.t51.aggregate([
{$group : {_id : null, x : {$push : "$x"}, y : {$push : "$y"}}},
{$project : {data : {
$map : {
input : {$range : [0, {$size : "$x"}]},
as : "idx",
in : { x : {$arrayElemAt : ["$x", "$$idx"]}, y : {$sum : {$slice : ["$y", {$sum : ["$$idx",1]}]}}}}
}}},
{$unwind : "$data"},
{$replaceRoot: {newRoot : "$data"}}
]).pretty()
如果您有更多要匯總或預測的字段,則可以在以下階段使用
db.t51.aggregate([
{$group : {_id : null, data : {$push : "$$ROOT"}}},
{$addFields : {data :
{$reduce : {
input : "$data",
initialValue : [{y : 0}],
in : {$concatArrays : [ "$$value",[{$mergeObjects : ["$$this", { y : {$sum : ["$$this.y",{$arrayElemAt : ["$$value.y", -1]}]}}]}]]}
}}
}},
{$addFields : {data : {$slice : ["$data", 1, {$size : "$data"}]}}},
{$unwind : "$data"},
{$replaceRoot: {newRoot : "$data"}}
]).pretty()
Aggregation Framework獨立地處理所有文檔(通過設計),因此它們彼此之間不了解。 更改的唯一方法是使用$ group 。 將_id
設置為null
可使您將所有文檔中的數據捕獲為一個。 然后,您可以遍歷該數組(使用$ range獲取索引數組,並使用$ map返回另一個數組)。 獲得x
( $ arrayElemAt )很容易,對於y
您需要使用$ reduce對所有數組元素求和。 $reduce
的輸入由$ slice運算符生成,並取決於索引(對於0
,它將是[10]
,對於1
[10,5]
,依此類推)。 要完成查詢,您需要$ unwind和$ replaceRoot以獲取原始文檔形狀。
db.col.aggregate([
{
$sort: { x: 1 }
},
{
$group: {
_id: null,
xArr: { $push: "$x" },
yArr: { $push: "$y" }
}
},
{
$project: {
data: {
$map: {
input: { $range: [ 0, { $size: "$xArr" } ] },
as: "i",
in: {
x: { $arrayElemAt: [ "$xArr", "$$i" ] },
y: {
$reduce: {
input: { $slice: [ "$yArr", { $add: [ "$$i", 1 ] } ] },
initialValue: 0,
in: { $add: [ "$$this", "$$value" ] }
}
}
}
}
}
}
},
{
$unwind: "$data"
},
{
$replaceRoot: {
newRoot: "$data"
}
}
])
我想您需要將上述步驟添加到現有聚合中。 為了證明它可以工作,可以將其運行為單獨的集合:
db.col.save({ x: '2019-01-09', y: 10 })
db.col.save({ x: '2019-01-10', y: 5 })
db.col.save({ x: '2019-01-11', y: 20 })
它輸出:
{ "x" : "2019-01-09", "y" : 10 }
{ "x" : "2019-01-10", "y" : 15 }
{ "x" : "2019-01-11", "y" : 35 }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.