'Mongodb update inside nested objects

code i am using to update using pymongo

    query = mycol.aggregate([
        {
                "$match": {
                    "structure.values.unique_report_name": report_name
                }
        },{
            "$replaceWith": {
                structure.values.report_data: new_data
            }
        }
    ])

    res = json.loads(dumps(query))
    return JsonResponse(res, safe=False)

My mongodb data, I am trying with replace with which is not working. I wants to replace 'report_data' with matching 'unique_report_name' But above query is not working

    [
        {
            "structure": [
                {
                    "name": "new dolfflf",
                    "values": [
                        {
                            "report_data": {
                                "ddddd": {
                                    "datasource": "newa"
                                },
                                "newsource": {
                                    "datasource": "dgr_data"
                                }
                            },
                            "unique_report_name": "newreportss"
                        },
                        {
                            "report_data": {
                                "kddkdkdd": {
                                    "datasource": "dgr_data",
                                }
                            },
                            "unique_report_name": "agaggag"
                        }
                    ]
                }
            ]
        }
    ]

expected result

    [
        {
            "structure": [
                {
                    "name": "new dolfflf",
                    "values": [
                        {
                            "report_data": {
                                "something": {
                                    "something": "something"
                                },
                            },
                            "unique_report_name": "newreportss"
                        },
                        {
                            "report_data": {
                                "something": {
                                    "something": "something",
                                }
                            },
                            "unique_report_name": "agaggag"
                        }
                    ]
                }
            ]
        }
    ]

Please take a look how can i solve this. Please add the query whwich will support pymongo also

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source