【问题标题】:How to sort data according to the status in mongoose aggregation如何根据猫鼬聚合中的状态对数据进行排序
【发布时间】:2020-08-07 11:55:59
【问题描述】:

我的佣金收藏中有很多记录。

{
    "_id": "5f2b96508082a30d27581cf7", 
    "createdAt": "2020-08-06T05:34:08.281Z",
    "supplierName": "John Cena", 
    "status": "Created", 
    "commissionID": "5f2b96508082a30d27581cf6"
},
{
    "_id": "5f2b996d8082a30d27581cf9",
    "createdAt": "2020-08-05T05:34:08.281Z", 
    "supplierName": "John Cena",
    "status": "Pending", 
    "commissionID": "5f2b96508082a30d27581cf6"
},
{
    "_id": "5f2ba2e28082a30d27581cfe", 
    "createdAt": "2020-08-05T05:34:08.281Z",
    "supplierName": "John Cena",
    "status": "Accepted", 
    "commissionID": "5f2b96508082a30d27581cf6"
},

{
    "_id": "5f2ba2ea8082a30d27581cff", 
    "createdAt": "2020-08-04T05:34:08.281Z",
    "supplierName": "John Cena",
    "status": "Pending", 
    "commissionID": "5f2b96508082a30d27581cf6"
},
{
    "_id": "5f2ba2ea8082a30d27581cff", 
    "createdAt": "2020-08-03T03:34:08.281Z",
    "supplierName": "John Cena",
    "status": "Rejected", 
    "commissionID": "5f2b96508082a30d27581cf6"
}

我需要在 desc 中按 Pending 状态和 createdAt 进行排序(我的意思是顶部的所有待处理记录以及 createdAt 日期 desc 顺序)

状态 = Created , Pending, Accepted, Rejected;

应该是这样的

{
    "_id": "5f2b996d8082a30d27581cf9",
    "createdAt": "2020-08-05T05:34:08.281Z", 
    "supplierName": "John Cena",
    "status": "Pending", 
    "commissionID": "5f2b96508082a30d27581cf6"
},
{
    "_id": "5f2ba2ea8082a30d27581cff", 
    "createdAt": "2020-08-04T05:34:08.281Z",
    "supplierName": "John Cena",
    "status": "Pending", 
    "commissionID": "5f2b96508082a30d27581cf6"
},
{
    "_id": "5f2b96508082a30d27581cf7", 
    "createdAt": "2020-08-06T05:34:08.281Z",
    "supplierName": "John Cena", 
    "status": "Created", 
    "commissionID": "5f2b96508082a30d27581cf6"
},
{
    "_id": "5f2ba2e28082a30d27581cfe", 
    "createdAt": "2020-08-05T05:34:08.281Z",
    "supplierName": "John Cena",
    "status": "Accepted", 
    "commissionID": "5f2b96508082a30d27581cf6"
},
{
    "_id": "5f2ba2ea8082a30d27581cff", 
    "createdAt": "2020-08-03T05:34:08.281Z",
    "supplierName": "John Cena",
    "status": "Rejected", 
    "commissionID": "5f2b96508082a30d27581cf6"
}

我正在使用 mongodb 3.4、mongoose 和 node.js。 请帮帮我。

【问题讨论】:

    标签: node.js mongodb mongoose aggregation-framework


    【解决方案1】:

    您可以通过自定义排序来做到这一点。

    play

    db.collection.aggregate([
      {
        $addFields: {
          sortId: {
            $cond: [
              {
                $eq: [ //If pending
                  "$status",
                  "Pending"
                ]
              },
              0, //then 0
              {
                $cond: [
                  { //Else if created
                    $eq: [
                      "$status", 
                      "Created"
                    ]
                  },
                  1, //then 1
                  2 //Else 2
                ]
              }
            ]
          }
        }
      },
      {
        $sort: {
          sortId: 1
        }
      }
    ])
    

    【讨论】:

    • 非常感谢@Gibbs
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2021-05-30
    • 1970-01-01
    • 2015-07-27
    • 2018-01-30
    • 2014-12-29
    • 2022-11-02
    相关资源
    最近更新 更多