如何在MongoDB聚合中添加自动递增字段? [英] How to add auto incrementing field in MongoDB aggregate?

查看:80
本文介绍了如何在MongoDB聚合中添加自动递增字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个附加的键号,该键号包含自动递增号的值.我怎样才能做到这一点?下面是查询.无需进行展开和分组是否可以实现此结果?

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
   }
]

推荐答案

这是不可能的.没有任何运算符可以生成序列号

It's not possible. There is no any operator to generate the sequence number

这篇关于如何在MongoDB聚合中添加自动递增字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆