I'm unable properly update document with another nested document. The simple example below explains in details. Any advise would be appreciated, thanks!
var ImageSchema = new Schema({ url: String });
var ProductSchema = new Schema({
images: {
"set1": [ ImageSchema ],
"set2": [ ImageSchema ],
// ... etc
});
var Product = db.model('Product', ProductSchema);
This works just fine:
var product = new Product({
images.set1 = [{ url: 'http://old.url.com' }],
images.set2 = [{ url: 'http://old.url.com' }],
// ... etc
});
product.save();
This fails:
Product.findOne(query, function(err, product){
product.images = {
"set1": [{ url: 'http://new.url.com' }],
"set2": [{ url: 'http://new.url.com' }]
}
product.save();
});
It would result the following document in DB:
{
"images": { "set1": [ null ], "set2": [ null ] }
// ... etc
}