Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
项目嵌入文档键值,基于mongoDB聚合中的条件_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

项目嵌入文档键值,基于mongoDB聚合中的条件

项目嵌入文档键值,基于mongoDB聚合中的条件,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我有一个名为tickets的mongo集合,我们将ticket详细信息存储在类似的结构文档中,如下所示: [ { "status": "PAUSED", "lifecycle_dates": { "OPEN": "d1", "CLOSED": "d2", "PAUSED": "d3" } }, { "status": "OPEN"

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

    [
      {
        "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"
        }
      }
    ]
{
    $project : {
        "status" : 1,
        "lifecycle_date" : $lifecycle_dates[$status]
    }
}
我需要获取显示票证当前状态和状态日期的数据

我想预测如下数据:

[
  {
    "status": "PAUSED",
    "lifecycle_date": "d3"
  },
  {
    "status": "OPEN",
    "lifecycle_date": "d1"
  },
  {
    "status": "CLOSED",
    "lifecycle_date": "d2"
  }
]
如何根据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"
        }
      }
    ]
{
    $project : {
        "status" : 1,
        "lifecycle_date" : $lifecycle_dates[$status]
    }
}
在中找不到任何引用或类似问题


当前mongo版本:3.2

您可以在下面进行尝试

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

更新答案:

由于需要根据
状态
获取
日期
,因此可以使用此聚合查询:

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

在此处使用
$project
<代码>[{$project:{status:1,lifecycle_dates:'$lifecycle_dates.PAUSED'}}]@AnthonyWinzlet抱歉,问题之前不清楚,我已经更新了它。你能再看一看吗?问题是我不知道当前的状态是什么,有三种状态可能是“打开”、“关闭”和“暂停”,它们各自的日期存储在一个名为“生命周期日期”的对象中。检索时,我只需要基于当前状态的生命周期日期。我已更新了我的答案,请根据您的更新进行验证。它与Mongo 3.2兼容。感谢您的回复,看起来应该是这样,但是Mongo 3.2中没有$objectToArray。在v3.2中有什么方法可以做到这一点吗?是的,您必须升级到3.6或更高版本才能使用它。。。在这里检查