如何在MongoDB中应用密集等级?

时间:2018-10-25 07:40:41

标签: mongodb mongoose mongodb-query aggregation-framework aggregate-functions

我是Mongo Db的新手,希望对此查询有所帮助。我的横断面ID和创建日期与

相同
      TRANSACTION_ID             CREATE_DATE   
 "0:ffff0a0a0983UY:-142"   "Fri Sep 07 16:51:29IST2018"
 "0:ffff0a0a0983UY:-142"    "Fri Sep 07 16:51:29IST2018"
 "0:ffff0a0a0983UY:-142"   "Fri Sep 07 16:51:29IST2018"
 "0:ffff0a0a0983UY:-111"   "Fri Sep 06 16:51:29IST2018"
 "0:ffff0a0a0983UY:-111"    "Fri Sep 06 16:51:29IST2018"

但是如何添加DENSE_RANK之类的

 DENSE_RANK         TRANSACTION_ID             CREATE_DATE   
     1          "0:ffff0a0a0983UY:-142"   "Fri Sep 07 16:51:29IST2018"
     1          "0:ffff0a0a0983UY:-142"   "Fri Sep 07 16:51:29IST2018"
     1          "0:ffff0a0a0983UY:-142"   "Fri Sep 07 16:51:29IST2018"
     2          "0:ffff0a0a0983UY:-111"   "Fri Sep 06 16:51:29IST2018"
     2          "0:ffff0a0a0983UY:-111"   "Fri Sep 06 16:51:29IST2018"

任何人请帮助我

1 个答案:

答案 0 :(得分:-1)

您可以使用更新查询;

db.test.update({"TRANSACTION_ID" : "0:ffff0a0a0983UY:-142"},{"$set" : {"DENSE_RANK" : 1}},{multi : true})

db.test.update({"TRANSACTION_ID" : "ffff0a0a0983UY:-111"},{"$set" : {"DENSE_RANK" : 2}},{multi : true})

输出为:

{
    "_id" : ObjectId("5bd188c34caa61c57872dbe7"),
    "TRANSACTION_ID" : "0:ffff0a0a0983UY:-142",
    "CREATE_DATE" : "Fri Sep 07 16:51:29IST2018",
    "DENSE_RANK" : 1.0
}

{
    "_id" : ObjectId("5bd188c34caa61c57872dbe8"),
    "TRANSACTION_ID" : "0:ffff0a0a0983UY:-142",
    "CREATE_DATE" : "Fri Sep 07 16:51:29IST2018",
    "DENSE_RANK" : 1.0
}

{
    "_id" : ObjectId("5bd188c34caa61c57872dbe9"),
    "TRANSACTION_ID" : "0:ffff0a0a0983UY:-142",
    "CREATE_DATE" : "Fri Sep 07 16:51:29IST2018",
    "DENSE_RANK" : 1.0
}
{
    "_id" : ObjectId("5bd189044caa61c57872dbec"),
    "TRANSACTION_ID" : "ffff0a0a0983UY:-111",
    "CREATE_DATE" : "Fri Sep 06 16:51:29IST2018",
    "DENSE_RANK" : 2.0
}

{
    "_id" : ObjectId("5bd189044caa61c57872dbed"),
    "TRANSACTION_ID" : "ffff0a0a0983UY:-111",
    "CREATE_DATE" : "Fri Sep 06 16:51:29IST2018",
    "DENSE_RANK" : 2.0
}