Pushing to array within object

I am terribly sorry if this is super basic. I have spent hours googling / trying and it seems I need some help.

Given…

    db.clients.find({name:"Aedan"}).pretty()
    {
            "_id" : ObjectId("60f0a879c1c9bb6a50854714"),
            "name" : "Aedan",
            "__v" : 0,
            "pmx" : {
                    "conditions" : [
                            {
                                    "name" : "",
                                    "status" : ""
                            }
                    ],
                    "surgeries" : [
                            {
                                    "name" : "",
                                    "date" : ""
                            }
                    ]
            }
    }

…I can’t figure out how to push an element to the “conditions” array.

For example, I’d like to push “name”:“hypertension” + “status”:“active”. (from mongo shell)

It seems like this should be doable or maybe I am wrong. I just can’t seem to get the syntax correct. Any ideas?

Array $push update operator will do the trick here.

db.clients.update( { "_id" : ObjectId("60f0a879c1c9bb6a50854714") }, { $push: { "pmx.conditions": { "name": "hypertension", "status": "active" }}} ) 

Thanks,
Mahi

1 Like

Thank you so much. It worked perfectly!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.