Search code examples
pythonmongodbpymongo

How to update a array value inside a dict value


enter image description here

I have a dict with key and value. In value i have a dict with key and a array value again. I want to know how to append datas inside that array value with pymongo ?!

i = mycol2.find_one({"chat_action":{"$exists":True}})
mycol2.update_one(i,{"$push":{"chat_action":{"fake_play":76}}})

I try this but i got this error :

raise WriteError(error.get("errmsg"), error.get("code"), error) pymongo.errors.WriteError: The field 'chat_action' must be an array but is of type object in document {_id: ObjectId('62eaafa3886a0c39b0961b0a')}, full error: {'index': 0, 'code': 2, 'errmsg': "The field 'chat_action' must be an array but is of type object in document {_id: ObjectId('62eaafa3886a0c39b0961b0a')}"}


Solution

  • You need to fully qualify the field with dot notation:

        {"$push": {"chat_action.fake_play": 76 } }
    

    From the docs,

    To specify a field in an embedded document or in an array, use dot notation.