I am looking to calculate the total count of users based on the previous month's count data.
Here is an example of how the results array should be structured:
RESULTS [
{ count: 1, accumulatedCount: 1, month: 12, year: 2021, verified: true },
{ count: 3, accumulatedCount: 4, month: 1, year: 2022, verified: true },
{ count: 3, accumulatedCount: 7, month: 2, year: 2022, verified: true },
{ count: 1, accumulatedCount: 8, month: 3, year: 2022, verified: true },
]
Currently, my aggregation pipeline is structured as follows:
const results = await this.accountModel.aggregate([
{
$match: {
...match,
createdAt: {
// $gte: range.from,
$lte: range.to,
},
},
},
{ $unwind: '$organizations' },
{
$group: {
_id: {
month: { $month: '$createdAt' },
year: { $year: '$createdAt' },
verified: '$organizations.verified',
},
count: { $sum: 1 },
},
},
{
$project: {
_id: 0,
month: '$_id.month',
year: '$_id.year',
count: 1,
verified: '$_id.verified',
},
},
]);
The current output of the aggregation pipeline includes the following:
RESULTS [
{ count: 1, month: 10, year: 2022, verified: true },
{ count: 4, month: 7, year: 2022, verified: true },
{ count: 3, month: 2, year: 2022, verified: true },
{ count: 1, month: 3, year: 2022, verified: true },
{ count: 1, month: 12, year: 2021, verified: true },
{ count: 2, month: 1, year: 2022, verified: true },
{ count: 1, month: 8, year: 2022, verified: true }
]
Essentially, I need to utilize a reduce
function to aggregate the count values based on previous and current data.
I have not found appropriate options for this in the MongoDB documentation.
The version of "mongodb" package I am using is "3.6.3".