简体   繁体   中英

MongoDB Aggregation Sum Growth

I have a MondoDB aggregation pipe that outputs the daily amount of revenue by aggregating all 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',
            },
        },
    ])

The output of this pipe might look like this:

[
    {
        x: '2019-01-09',
        y: 10,
    },
    {
        x: '2019-01-10',
        y: 5,
    },
    {
        x: '2019-01-11',
        y: 20,
    }
]

That means on 2019-01-09 there were sales with the amount of $10, on 2019-01-10 $5, etc.

Now, I don't want the daily earnings, but the sum of all earnings ever made. That means the result should look like this:

[
    {
        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)
    }
]

So basically I don't want the daily amount but the growth.

PS: I am using this data to be displayed in a chart.

You can add the following stages in addition to what you have

logic is $push x and y to array, iterate the array using $range by index, for x get $arrayElemAt at index , for y $slice and $sum till 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()

if you have more fields to be aggregated or projected you can use below stages

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 processes all the documents independently (by design) so they don't know about each other. The only way to change that is to use $group . Setting _id to null allows you to capture the data from all documents into single one. Then you can iterate through that arrays (using $range to get the array of indexes and $map to return another array). It's easy to get x ( $arrayElemAt ) and for y you need to sum all array elements using $reduce . The input for $reduce is generated by $slice operator and depends on an index (for 0 it will be [10] , for 1 [10,5] and so on). To finalize the query you need $unwind and $replaceRoot to get original document shape.

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"
        }
    }
])

I guess you need to append above steps to your existing aggregation. To prove that it works you can run it for separate collection:

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 })

and it outputs:

{ "x" : "2019-01-09", "y" : 10 }
{ "x" : "2019-01-10", "y" : 15 }
{ "x" : "2019-01-11", "y" : 35 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM