问题
I want to add an additional key number which holds a value of the auto-incrementing number. How can I achieve this? Below is the query. Is it possible to achieve this result without doing unwind and group?
[
{
"$match":{
"iContestId":"5e4118161a7b5765ee9b40ef"
}
},
{
"$project":{
"_id":"$_id",
"nPointsEarned":"$nPointsEarned",
"iContestId":"$iContestId",
"iUserId":"$iUserId",
"ranking":"$ranking",
"sFullName":"$sFullName",
"sProfilePicture":"$sImage",
"nAutoIncrement": {
"$count": {
"$sum": 1
}
}
}
},
{
"$sort":{
"ranking":1
}
},
{
"$skip":0
},
{
"$limit":23
}
]
回答1:
It's not possible. There is no any operator to generate the sequence number
来源:https://stackoverflow.com/questions/61422659/how-to-add-auto-incrementing-field-in-mongodb-aggregate