I'm sorry if title isn't clear I didn't know how else to put this.
Let's say I have the following schema:
{
name: {
type: String
},
additionalInfo: {
someInfo: {
a: {
type: String
},
b : {
type: String
}
}
}
}
Now say I want to make a function that changes field b
and ONLY b
.
function changeB(id, newB) {
return new Promise((resolve, reject) => {
myModel.findByIdAndUpdate(id, { additionalInfo: { someInfo: { b } } }, (err, doc) => {
if(err) reject(err);
if(doc) resolve(doc);
reject();
});
});
}
Problem here is that function overwrites entire additionalInfo
for that document. So if I had a a
value in it as well, after running that function it'll be gone. What should I do to only change additionalInfo.someInfo.b
?
Actually while writing this question (especially on that last sentence) I figured it out myself. Following function only changes additionalInfo.someInfo.b
without deleting anything else.
function changeB(id, newB) {
return new Promise((resolve, reject) => {
myModel.findByIdAndUpdate(id, { 'additionalInfo.someInfo.b': newB }, (err, doc) => {
if(err) reject(err);
if(doc) resolve(doc);
reject();
});
});
}
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.