mongodb聚合中的$ concat字符串和$ cond

时间:2018-08-09 11:35:41

标签: mongodb aggregation-framework mongodb4.0

[
{
    "user_id" : 12453,
    "profile_type" : "demo_type_1",
    "records" : [
        {
            "type" : "typ_11",
            "value" : {
                "high" : 115,
                "low" : 78
            },
            "_meta" : {
                "data_type" : "text"
            }
        },
        {
            "type" : "type_1",
            "files" : [
                {
                    "title" : "path_prescription_1",
                    "url" : "/file_name.extension"
                },
                {
                    "title" : "path_prescription_2",
                    "url" : "/file_name__1.extension"
                }
            ],
            "_meta" : {
                "data_type" : "file"
            }
        }
    ]
},
{
    "user_id" : 12455,
    "profile_type" : "demo_type_1",
    "records" : [
        {
            "type" : "typ_11",
            "value" : {
                "high" : 115,
                "low" : 78
            },
            "_meta" : {
                "data_type" : "text"
            }
        },
        {
            "type" : "type_1",
            "files" : [
                {
                    "title" : "path_prescription_1",
                    "url" : "/file_name.extension"
                },
                {
                    "title" : "path_prescription_2",
                    "url" : "/file_name__1.extension"
                }
            ],
            "_meta" : {
                "data_type" : "file"
            }
        }
    ]
},
...
]

仅当_meta字段data_type是非文本文件时,我才想在URL值前添加前缀以使其成为绝对路径。将以上述格式存储,并仅以附加的网址以相同的格式进行检索。

有什么办法可以通过使用聚合管道来做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以尝试像这样使用$addFields

db.getCollection('test').aggregate(

[

{
    $unwind: {
        path:"$records",

        preserveNullAndEmptyArrays: true
        }
},

{
    $unwind: {
        path:"$records.files",

        preserveNullAndEmptyArrays: true
        }
},

{
    $addFields: {
        "records.files.url":{
             $cond: {
                    if: {
                      $eq: ['$records.files', undefined]
                    },
                    then: null,
                    else: {$concat: ["some_prefix", "$records.files.url"]}
              }

            }
        }

    }

]

)

这将为您提供:

/* 1 */
{
    "_id" : ObjectId("5b6c484b9a8ea6a11c508520"),
    "user_id" : 12453.0,
    "profile_type" : "demo_type_1",
    "records" : {
        "type" : "typ_11",
        "value" : {
            "high" : 115.0,
            "low" : 78.0
        },
        "_meta" : {
            "data_type" : "text"
        },
        "files" : {
            "url" : null
        }
    }
}

/* 2 */
{
    "_id" : ObjectId("5b6c484b9a8ea6a11c508520"),
    "user_id" : 12453.0,
    "profile_type" : "demo_type_1",
    "records" : {
        "type" : "type_1",
        "files" : {
            "title" : "path_prescription_1",
            "url" : "some_prefix/file_name.extension"
        },
        "_meta" : {
            "data_type" : "file"
        }
    }
}

/* 3 */
{
    "_id" : ObjectId("5b6c484b9a8ea6a11c508520"),
    "user_id" : 12453.0,
    "profile_type" : "demo_type_1",
    "records" : {
        "type" : "type_1",
        "files" : {
            "title" : "path_prescription_2",
            "url" : "some_prefix/file_name__1.extension"
        },
        "_meta" : {
            "data_type" : "file"
        }
    }
}

/* 4 */
{
    "_id" : ObjectId("5b6c484b9a8ea6a11c508521"),
    "user_id" : 12455.0,
    "profile_type" : "demo_type_1",
    "records" : {
        "type" : "typ_11",
        "value" : {
            "high" : 115.0,
            "low" : 78.0
        },
        "_meta" : {
            "data_type" : "text"
        },
        "files" : {
            "url" : null
        }
    }
}

/* 5 */
{
    "_id" : ObjectId("5b6c484b9a8ea6a11c508521"),
    "user_id" : 12455.0,
    "profile_type" : "demo_type_1",
    "records" : {
        "type" : "type_1",
        "files" : {
            "title" : "path_prescription_1",
            "url" : "some_prefix/file_name.extension"
        },
        "_meta" : {
            "data_type" : "file"
        }
    }
}

/* 6 */
{
    "_id" : ObjectId("5b6c484b9a8ea6a11c508521"),
    "user_id" : 12455.0,
    "profile_type" : "demo_type_1",
    "records" : {
        "type" : "type_1",
        "files" : {
            "title" : "path_prescription_2",
            "url" : "some_prefix/file_name__1.extension"
        },
        "_meta" : {
            "data_type" : "file"
        }
    }
}

但是请记住,当记录的类型为 text 时,请忽略records.files.url字段。