Insert new entry into MongoDB array field using mongolite update/upsert

I want to insert new objects into a MongoDB document's array field.

Initially it look like this:
image

I want to insert a new Object into "notifications" (Array) field.
After upsert, It should look like this:
image

I'm using below format code

# conn_usr is a mongolite's mongo connection object
conn_usr$update(query  = '{ "user": "user" }',
                update = '{ "$set": {"notifications":[{"key1": "x",  "key2": "y"}] } }',
                upsert = TRUE  ) # 

This code replaces the first item, not inserting into "notifications" field.
Any help is appreciated.

Resolved. Need to use $push within command.

conn_usr$update(query  = '{ "user": "user" }',
                update = '{ "$push": {"notifications": {"key1": "x",  "key2": "y"} } }'  )
1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.