Home > Back-end >  pymongo update nested data key
pymongo update nested data key

Time:02-25

pymongo query

    mycol.update(
        {
            "unique_report_id": 330665,
            "structure.name": "sommememe",
            "structure.values.unique_report_name": "areatest"
        },
        {
            "$set": {
                    "structure.$.values.$.report_data": {
                        data
                }
            }
        },
        {
            "multi": True
        }
    )

data

[
    {
        "structure": [
            {
                "name": "sommememe",
                "values": [
                {
                    "report_name": "area test",
                    "report_heading": "area test",
                    "background": "#f0f2f5",
                    "grid": false,
                    "report_data": {
                    "slslsllsls": {
                        "datasource": "alias",
                        "source": "3",
                        "title": "slslsllsls",
                        "chartColor": "#00D88A",
                        "area": {
                        "x": 10,
                        "y": 10,
                        "width": 360,
                        "height": 360
                        }
                    }
                    },
                    "unique_report_name": "areatest"
                }
                ],
                "type": 1
            }
        ],
        "unique_report_id": 330665
    }
]

I wants to replace report_data key with data coming from api I am trying above query but it is not working,

Please take a look how can i fix it.

Thanks

I added data again. Please check

    Traceback (most recent call last):
    File "/Users/soubhagyapradhan/Desktop/upwork/report/backend/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
        response = get_response(request)
    File "/Users/soubhagyapradhan/Desktop/upwork/report/backend/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/Users/soubhagyapradhan/Desktop/upwork/report/backend/api/views.py", line 314, in create_report
        "structure.$.values.$.report_data": {
    TypeError: unhashable type: 'dict'

I am getting above error when i am running above query on pymongo. Please check what is wrong here.

CodePudding user response:

db.collection.update({
  "structure.values.unique_report_name": "areatest"
},
{
  "$set": {
    "structure.$[].values.$[ele].report_data": {
      "slslsllsls": {
        "datasource": "aliasqqqqqq",
        "source": "3333333",
        "title": "slslsllslsqqqqqq",
        "chartColor": "#00D88A",
        "area": {
          "x": 10000,
          "y": 10000,
          "width": 9999,
          "height": 9999
        }
      }
    }
  }
},
{
  arrayFilters: [
    {
      "ele.unique_report_name": "areatest"
    }
  ]
})

mongoplayground

  • Related