Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$concat string with $cond in mongodb aggregation

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

i want to append prefix the url value to make it an absolute path while retrieving from the database only when the _meta fields data_type is file not for text. The is stored in the above format and retrieved in the same format only with the appended url.

is there any way to do this by using aggregation pipeline ?

like image 903
infinitywarior Avatar asked Jan 23 '26 23:01

infinitywarior


1 Answers

You can try using $addFields like this:

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

            }
        }

    }

]

)

This will give you:

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

But remember to ignore records.files.url field when the record is type text.

like image 84
dsharew Avatar answered Jan 26 '26 15:01

dsharew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!