Ta agregacja dodaje pole z licznikiem:
db.collection.aggregate( [
{
$match: {
joining_date: { $exists: true }
}
},
{
$group: {
_id: null,
docs: { $push: "$$ROOT" }
}
},
{
$project: {
_id: 0,
R: {
$map: {
input: { $range: [ 0, { $size: "$docs" } ] },
in: {
$mergeObjects: [
{ joining_date_count: { $add: [ "$$this", 1 ] } },
{ $arrayElemAt: [ "$docs", "$$this" ] }
]
}
}
}
}
},
{
$unwind: "$R"
},
{
$replaceRoot: { newRoot: "$R" }
}
] )