Denne sammenlægning tilføjer et felt med en tæller:
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" }
}
] )