When I run this command:
var news = {
_id: req.body.newsItemId,
images: {
_id: new ObjectId(),
src: result
}
}
Shop.findOneAndUpdate({
_id: ObjectId(req.body.shopId),
'news._id': ObjectId(req.body.newsItemId)},
{"$set": {
"news.$": news
}
}).exec(function(err, shop){
console.log(err, shop);
})
The non existing field from the news
object are overwritten with empty values, so they are removed from the document.
news is a subdocument from shops. So a document looks like:
name: 'test shop',
email: 'email@shop',
products: [],
news: [
{
name: 'test news',
content: 'lorum ipsum....',
summary: 'lorum',
images: [
{
src: 'news/image_1.jpg',
}
]
}
]
After running the command above the news contains only a id and 1 image. I want only to overwrite the images array and leave the other properties alone. I have tried it with only images in the set, but then I get this error Mongoose update 'cannot use the part (..) to traverse the element , that is where the $ comes from.