Home:ALL Converter>Add element to an array embedded inside a sub-document in MongoDB

Add element to an array embedded inside a sub-document in MongoDB

Ask Time:2021-06-05T22:13:51         Author:Jose Alberto

Json Formatter

I have the following scheme on my MongoDB

{
    "weekday": 0,
    "hour":[
            {
                "hour": 14,
                "user": "user",
                "promoted": ["user1","user2","user3","user4"]
            },
            {
                "hour": 15,
                "user": "user",
                "promoted": ["user1","user2","user3","user4"]
            },
    ]
}

I want to add single elements to the "promoted" array of a certain hour, for example, add "user5" to the "promoted" array of the sub-document which has "hour": 14.

I have learned how to add new sub-documents to the hour array but not how to add a single element to the "promoted" array of a already existing sub-document. How could I do that?

Author:Jose Alberto,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/67850471/add-element-to-an-array-embedded-inside-a-sub-document-in-mongodb
yy