I have a collection containing documents as such:
{
"category":"A",
"list": [
{
"item": 1,
"sub_list": [ 11 ]
},
{
"item": 2,
"sub_list": [13, 43]
},
],
}
How do I add a number to the sub_list
given category
& item
? and if it doesn’t exist then upsert and create
db.collection.update({
"category": "A", // "B" will create/upsert new document
"list.item": 1 // 2 will add new obj to list
},
???
{
upsert: true
})
2
Answers
Tried to work with a solution.
Got something similar as Umer Abbas comment.
You can try it on mongoplayground.
EDITED ANSWER
To push a number to the sub_list, we require to do the following: