How to add auto incrementing field in MongoDB aggregate?

佐手、 提交于 2021-02-11 15:41:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!