The question is how I can refer to the previous document from the result set?
I have an aggregation with such result:
var result = db.collection.aggregate([...])
{
"_id" : "2018-01",
"month" : "2018-01",
"count" : 283.0
}
{
"_id" : "2018-02",
"month" : "2018-02",
"count" : 260.0
}
{
"_id" : "2018-03",
"month" : "2018-03",
"count" : 287.0
}
{
"_id" : "2018-04",
"month" : "2018-04",
"count" : 264.0
}
{
"_id" : "2018-05",
"month" : "2018-05",
"count" : 292.0
}
The goal is to get the difference between count in this month and count in the previous one. So to have results like this:
{
"_id" : "2018-01",
"month" : "2018-01",
"count" : 283.0,
"difference" : 283.0
}
{
"_id" : "2018-02",
"month" : "2018-02",
"count" : 260.0,
"difference" : -23.0
}
{
"_id" : "2018-03",
"month" : "2018-03",
"count" : 287.0,
"difference" : 17.0
}