根据mongoDB聚合中的条件,投影嵌入式文档的键值

时间:2018-07-24 08:00:19

标签: mongodb mongodb-query aggregation-framework

我有一个称为票证的mongo集合,我们将票证详细信息存储在类似的结构文档中,如下所示:

    [
      {
        "status": "PAUSED",
        "lifecycle_dates": {
          "OPEN": "d1",
          "CLOSED": "d2",
          "PAUSED": "d3"
        }
      },
      {
        "status": "OPEN",
        "lifecycle_dates": {
          "OPEN": "d1",
          "PAUSED": "d3"
        }
      },
      {
        "status": "CLOSED",
        "lifecycle_dates": {
          "OPEN": "d1",
          "CLOSED": "d2"
        }
      }
    ]

我需要获取表明票证当前状态和日期的数据。

并且我想要投影数据:

[
  {
    "status": "PAUSED",
    "lifecycle_date": "d3"
  },
  {
    "status": "OPEN",
    "lifecycle_date": "d1"
  },
  {
    "status": "CLOSED",
    "lifecycle_date": "d2"
  }
]

如何根据mongo聚合管道中的当前状态预测单个生命周期日期? 像这样的东西:

{
    $project : {
        "status" : 1,
        "lifecycle_date" : $lifecycle_dates[$status]
    }
}

mongo reference document here

中找不到任何参考或类似问题

当前的mongo版本:3.2

3 个答案:

答案 0 :(得分:2)

更新后的答案:

由于您需要按照date来获取status,因此可以使用以下聚合查询:

db.test.aggregate([ 
{
    $project : {
        _id : 0,
        status : 1,
        lifecycle_date : { $cond: [ {$eq : ["$status","OPEN"]}, "$lifecycle_dates.OPEN", { $cond: [ {$eq : ["$status","CLOSED"]}, "$lifecycle_dates.CLOSED", { $cond: [ {$eq : ["$status","PAUSED"]}, "$lifecycle_dates.PAUSED", "-1" ]} ]} ]}
    }
}]) 

这也与Mongo 3.2兼容。

输出:

{ "status" : "PAUSED", "lifecycle_date" : "d3" }
{ "status" : "OPEN", "lifecycle_date" : "d1" }
{ "status" : "CLOSED", "lifecycle_date" : "d2" }

================================================ ========================

此答案是针对先前的问题-

使用此聚合:

db.test.aggregate([
{
    $project : {
        _id : 0,
        status : 1,
        lifecycle_date : "$lifecycle_dates.PAUSED"
    }
}
])

输出:

{ "status" : "PAUSED", "lifecycle_date" : "d3" }

答案 1 :(得分:1)

您可以尝试以下汇总

db.collection.aggregate([
  { "$project": {
    "status": 1,
    "lifecycle_date": {
      "$arrayElemAt": [
        { "$filter": {
          "input": { "$objectToArray": "$lifecycle_dates" },
          "as": "life",
          "cond": { "$eq": ["$$life.k", "$status"] }
        }},
        0
      ]
    }
  }},
  { "$project": {
    "status": 1,
    "lifecycle_date": "$lifecycle_date.v"
  }}
])

答案 2 :(得分:0)

db.tickets.aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $project: {
                "status": 1,
                _id: 0,
                "lifecycle_dates": {
                    $switch: {
                        branches: [{
                                case: {
                                    $eq: ["$status", "PAUSED"]
                                },
                                then: "$lifecycle_dates.PAUSED"
                            },
                            {
                                case: {
                                    $eq: ["$status", "OPEN"]
                                },
                                then: "$lifecycle_dates.OPEN"
                            },
                            {
                                case: {
                                    $eq: ["$status", "CLOSED"]
                                },
                                then: "$lifecycle_dates.OPEN"
                            }
                        ],

                    }
                }
            }
        },
    ])