Pymongo how to properly use $push to update an existing document

$push does not work in this case because you are trying to use an array function on an object.

To use $push you would need to change your data structure to the following:

{
    "_id" : ObjectId("501c83051d41c8753e000000"), 
    "node" : "denver", 
    "enc" : {
        "environment" : "production", 
        "classes" : [
            "denver"
        ]
    }, 
    "inherit" : "default"
}

Then your query would be:

col.update(
    {
        'node' : 'denver'
    },
    {
        '$push': {
            'enc.classes' : 'boulder'
        }
    },
    True
)

This query works.

db.foo.update({"node": "denver"}, {"$set": {"enc.classes.boulder": ""}}

Tags:

Pymongo