I have a PATCH method for my API to update DB content in MongoDB. The DB schema has a nested structure and updating using the below method is replacing the whole set where few fields are removed.
The DB structure looks like
{
_id : ObjectId(1234...),
name : "name",
age : 30,
levelone : [{
"levelone": [{
"name": "name1",
"status": 1
}, {
"name": "name2",
"status": 2
}, {
"name": "name3",
"status": 1
}],
"levelone": "one",
"levelonename": "abcdefgh"
},
{
"leveltwo": [{
"name": "name1",
"status": 1
}, {
"name": "name2",
"status": 2
}],
"levelone": "two",
"levelonename": "asdadadasd"
}]
}
the update document structure looks like
{
_id : ObjectId(1234...),
name : "name",
age : 30,
levelone : [{
"levelone": [{
"name": "name1",
"status": 1
}, {
"name": "name2",
"status": 2
}, {
"name": "name3",
"status": 1
}] },
{
"leveltwo": [{
"name": "name1",
"status": 1
}, {
"name": "name2",
"status": 2
}]
}]
}
The node js MongoDB query looks like
findByIdAndUpdate({'_id': id},{$set: updateObject});
the query removes the values levelone and levelonename and replaces the levelone.levelone, i want just to update the values of object levelone alone. Please advise