I am looking for a way to update a field within a nested object in an array using one MongoDB query.
Consider the following schema:
var PlatformPhotoAlbumSchema = new Schema({
platformAlbumId: String,
platformPhotoIds: [String]
}, { _id : false });
var SocialProfileSchema = new Schema({
platformPhotoAlbums: [PlatformPhotoAlbumSchema]
});
For example:
{
platformPhotoAlbums: [
{
platformAlbumId: "a",
platformPhotoIds: ["1","2"]
},
{
platformAlbumId: "b",
platformPhotoIds: ["3","4"]
},
{
platformAlbumId: "c",
platformPhotoIds: ["5","6"]
}
]
}
- How can I update the object with platformAlbumId "c" to have platformPhotoIds: ["5","6","7"]?
- If no document exists with platformAlbumId as "c", then how can I create a new object with platformAlbumId: "c" and platformPhotoIds: ["5","6","7"] and add it to platformPhotoAlbums?
I want to achieve this using a single mongodb query. Do you have any suggestions on how to accomplish this?