I want to perform an update on a field which is present in a nested object in an array.
Consider this:
var PlatformPhotoAlbumSchema = new Schema({
platformAlbumId: String,
platformPhotoIds: [String]
}, { _id : false });
var SocialProfileSchema = new Schema({
platformPhotoAlbums: [PlatformPhotoAlbumSchema]
});
Example:
{
platformPhotoAlbums: [
{
platformAlbumId: "a",
platformPhotoIds: ["1","2"]
},
{
platformAlbumId: "b",
platformPhotoIds: ["3","4"]
},
{
platformAlbumId: "c",
platformPhotoIds: ["5","6"]
}
]
}
- How can I update an existing object, for e.g., platformAlbumId with "c" with platformPhotoIds: ["5","6","7"]
- If there is no document with platformAlbumId with "c", then create a new object with platformAlbumId as "c" and platformPhotoIds: ["5","6","7"] and insert into platformPhotoAlbums.
I want to do this in one single mongodb query. Any idea how can this be done ?