Answer the question
In order to leave comments, you need to log in
How to add a field to a MongoDB subdocument without deleting already existing fields?
Help, I don't understand the logic!
I have a collection of photo galleries and each can have an array of albums.
Both galleries and albums can have their own thumbnail image to display in the list.
I add the image field to the gallery with no problems, and everything merges fine:
db.photos.update({_id:"1587050015794"},{$set:{image:"image.link"}})
db.photos.update({"_id" : "1587050015794","albums._id": "1587050121637"},{$set: {'albums.$':{image:"image.link"}}})
{
"_id" : "1587050015794",
"slug" : "1",
"description" : {
"fr" : "1",
"ru" : "1"
},
"albums" : [
...,
{
"_id" : "1587050121637",
"slug" : "11",
"description" : {
"fr" : "11",
"ru" : "11"
},
"gallery" : [ ]
},
...
}
Answer the question
In order to leave comments, you need to log in
to add to $push array
https://docs.mongodb.com/manual/reference/operator...
db.photos.update(
{_id:"1587050015794"},
{ $push: {
albums: { slug: 6666666 }
}
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question