Assume I have the following document:
{ name: 'any-name', array: [ { _id: 'any-id', a: 'any-a-data', b: 'any-b-data', c: 'any-c-data' }, ...more objects ] }
I want to call updateOne method and update the entire object in array with _id = 'any-id' with the folowing object
{ a: 'new-a-data', b: 'new-b-data', c: 'new-c-data' }
so the result will be:
{
name: 'any-name',
array: [
{ _id: 'any-id', a: 'new-a-data', b: 'new-b-data', c: 'new-c-data' },
...more objects
]
}
I trying something like:
model.updateOne( { 'array._id': _id }, { $set: { 'array.$': data } } )
data = { a: 'new-a-data', b: 'new-b-data', c: 'new-c-data' }
can someone help me?
You need to parse the new data before updating the document:
var { newA, newB } = data;
Model.updateOne({ 'array._id': 'any-id' }, {
'$set': {
'array.$.a': newA,
'array.$.b': newB
}
})
.then((data) => { console.log(data); res.redirect('/') })
.catch((err) => res.status(500).send("Error: ", err));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.