如何使用Mongodb聚合管道添加RNUM

时间:2018-10-24 11:36:14

标签: mongodb mongoose mongodb-query

我是Mongo Db的新手,希望对此查询有所帮助。

 DATA:



   {
        "RESOURCE_ID" : "100-101",
        "TRANSACTION_ID" : "0:ffff0a0a0983UY:-111",
        "CREATE_DATE" : "Fri Sep 07 16:51:29 IST 2018"
    }
    {
        "RESOURCE_ID" : "100-101",
        "TRANSACTION_ID" : "0:ffff0a0a0983UY:-111",
        "CREATE_DATE" : "Fri Sep 07 16:51:29 IST 2018"
    }
    {
        "RESOURCE_ID" : "100-101",
        "TRANSACTION_ID" : "0:ffff0a0a0983UY:-111",
        "CREATE_DATE" : "Fri Sep 07 16:51:29 IST 2018"
    }
    {
        "RESOURCE_ID" : "100-102",
        "TRANSACTION_ID" :"0:ffff0a0a0983UY:-142",
        "CREATE_DATE" : "Fri Sep 06 16:51:29 IST 2018"
    }
    {
        "RESOURCE_ID" : "100-102",
        "TRANSACTION_ID" :"0:ffff0a0a0983UY:-142",
        "CREATE_DATE" : "Fri Sep 06 16:51:29 IST 2018"
    }
    {
        "RESOURCE_ID" : "100-102",
        "TRANSACTION_ID" :"0:ffff0a0a0983UY:-142",
        "CREATE_DATE" : "Fri Sep 06 16:51:29 IST 2018"
    }

我写了以下聚合管道获取字段名,但是如何添加Rnum例如具有相同日期的相同TRANSACTION_ID 3条记录有1条记录号rownumber 1条提醒2条记录行号“ NUll”基于CREATEDATE的任何一个,请建议我。

 db.COLLECTION_DETAILS_T.aggregate([{

  {"$project": {
      _id:0,
     "RESOURCE_ID": 1,
     "TRANSACTION_ID":1,
     "CITY_ID" : 1,
     "EVENT":1,
     "CREATE_DATE":1,
     "MODULE_ID":1,
   }},

  ]) 

预期输出:robomongo表视图

1    RNUM   RESOURCE_ID        TRANSACTION_ID             CREATE_DATE   
2     1     "100-101"      "0:ffff0a0a0983UY:-142"  "Fri Sep 07 16:51:29IST2018"
3    NULL   "100-101"      "0:ffff0a0a0983UY:-142"  "Fri Sep 07 16:51:29IST2018"
4    NULL   "100-101"      "0:ffff0a0a0983UY:-142"  "Fri Sep 07 16:51:29IST2018"
5     2     "100-102"      "0:ffff0a0a0983UY:-111"  "Fri Sep 06 16:51:29IST2018"
6    NULL   "100-102"      "0:ffff0a0a0983UY:-111"  "Fri Sep 06 16:51:29IST2018"

0 个答案:

没有答案