Using an aggregation, is there a way to group by year and sum the document count, but additionally add the sum of all later years?
The result I want is this:
[
{
_id: "2012",
count: 4 // years 2012-2014
},
{
_id: "2013",
count: 2 // years 2013-2014
},
{
_id: "2014",
count: 1 // only year 2014
}
]
Right now, I’m using a normal $group + $sum, which gives me the counts for each year individually and then I sort them in JavaScript. I was hoping that there was a simpler way that gets rid of the additional JS code:
However, I’m getting an error that I can’t find any results for in Google:
MongoServerError: PlanExecutor error during aggregation :: caused by :: Requested document not in SpillableCache. Expected range was 0--1 but got 0
[...]
code: 5643004,
codeName: 'Location5643004'
yearCounts is part of a facet. There are two more facet stages coming after it. The error disappears when I put yearCountsafter these two. But why is that happening? I thought these facet stages are independent of each other?