I would like to ask you for help in finding the maximum dates (limit 3) from all groups.
collection.aggregate([
{
"$group": {
"_id": "$flashcardCollection",
"finalMaxDate": { "$max": "$sessionDate" }
},
},
{ "$sort": { "finalMaxDate": 1 } },
{ "$limit": 3 }
]).exec(function(error, fetchAllRecords){
console.log(fetchAllRecords);
});
my code finds only one maximum date in each group even though the limit is set to 3
Try this:
db.testCollection.aggregate([
{ $sort: { finalMaxDate: -1 } },
{
$group: {
_id: "$flashcardCollection",
finalMaxDate: { $push: "$finalMaxDate" }
}
},
{
$project: {
finalMaxDate: {
$slice: ["$finalMaxDate", 3]
}
}
}
])