2
[
{
    "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"
            }
        }
    ]
},
...
]

我想在 url 值附加前缀以使其成为绝对路径,同时仅当 _meta 字段 data_type 是文件而不是文本时才从数据库中检索。以上述格式存储,并且仅使用附加的 url 以相同的格式检索。

有没有办法通过使用聚合管道来做到这一点?

4

1 回答 1

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

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

于 2018-08-09T14:14:39.587 回答