展平数组mongodb $ project

时间:2018-11-14 19:20:18

标签: mongodb

我有一个名为Patient的mongodb集合-包含数百个文档,其中每个文档代表1个人。每个文档包含许多数组-其中一个数组代表有关患者的信息类别。这里的示例-显示根级别信息(_id,EnrollmentID)和2个数组:成像和健康状态。

/* 1 */
{
    "_id" : ObjectId("5be32372ca69786f080f36a1"),
    "EnrollmentId" : 1234,
    "imaging" : [
        {
            "EnrollmentId" : 1234,
            "CR_ImagingXRay" : "1 Time",
            "CR_ImagingCT" : "1 Time",
            "CR_ImagingMRI" : "1 Time",
        }
    ],
    "health_state" : [
        {
            "EnrollmentId" : 1234,
            "CR_DateAssessmentCompletion" : "14-Jan-16",
            "CR_HealthState" : 55.0
        },
        {
            "EnrollmentId" : 1234,
            "CR618_DateAssessmentCompletion" : "27-Apr-16",
            "CR618_HealthState" : NaN,
            "CR_CR618HealthStateChange" : NaN
        },
        {
            "EnrollmentId" : 1234,
            "CR12_DateAssessmentCompletion" : NaN,
            "CR12_HealthState" : NaN,
            "CR_CR12HealthStateChange" : NaN
        },
        {
            "EnrollmentId" : 1234,
            "CR24_DateAssessmentCompletion" : "8-Jan-18",
            "CR24_HealthState" : 85.0,
            "CR_CR24HealthStateChange" : 30.0
        }
    ]
}

我想使用聚合管道来创建仅包含主文档特定元​​素的新文档。我写了这个查询:

db.patient.aggregate([
    {
        $project: 
            {_id: 0, EnrollmentId: 1,
             CR_HealthState: '$health_state.CR_HealthState',
             CR618_HealthState: '$health_state.CR618_HealthState',
             CR12_HealthState: '$health_state.CR12_HealthState',
             CR24_HealthState: '$health_state.CR24_HealthState'
            }},
   {
        $out: "test"
   }
   ])

给出以下输出:

/* 1 */
{
    "_id" : ObjectId("5bec716cb75cface16e9e88c"),
    "EnrollmentId" : 1234,
    "CR_HealthState" : [
        55.0
    ],
    "CR618_HealthState" : [
        NaN
    ],
    "CR12_HealthState" : [
        NaN
    ],
    "CR24_HealthState" : [
        85.0
    ]
}

我希望像这样的“扁平”输出:

/* 1 */
{
    "_id" : ObjectId("5bec716cb75cface16e9e88c"),
    "EnrollmentId" : 1234,
    "CR_HealthState" : 55.0,
    "CR618_HealthState" : NaN,
    "CR12_HealthState" : NaN,
    "CR24_HealthState" : 85.0
}

我是mongodb的新手,我们将不胜感激!

1 个答案:

答案 0 :(得分:0)

由于您要在数组中投影元素,因此正如您所观察到的那样,投影本身就是数组。为避免这种情况,您可以使用$ arrayElemAt并引用数组的第一个元素0索引。见下文:

{'$project': {'EnrollmentId': 1,
             'CR_HealthState': {'$arrayElemAt': ['$health_state.CR_HealthState', 0]},
             'CR618_HealthState': {'$arrayElemAt': ['$health_state.CR618_HealthState', 0]},
             'CR12_HealthState': {'$arrayElemAt': ['$health_state.CR12_HealthState', 0]},
             'CR24_HealthState': {'$arrayElemAt': ['$health_state.CR24_HealthState', 0]},
            }},