{
"_id" : ObjectId("63a00087347cec20308de2ec"),
"registration" : {
"email" : "admin@admin.com",
"password" : "password"
},
"profile" : {
"name" : "My Name",
"mark" : "r4u4tlbOSq",
"category" : "general",
//other 60 fields
}
}
I want to update the sub-document “profile” with
//req.body
{ // name field not present
"mark" : "newmark",
"category" : "general",
//other 60 fields
}
The code I wrote below and it is deleting profile.name, but I want to preserve profile.name. I want to preserve all the fields that are either not present in req.body or is empty string. How can I do that.
const query = { _id: ObjectId(req.userid) };
const update = { $set: {"profile" :{...req.body}}};
const options = { upsert: true };
await colname.updateOne(query, update, options);
Actual Result
{
"_id" : ObjectId("63a00087347cec20308de2ec"),
"registration" : {
"email" : "admin@admin.com",
"password" : "password"
},
"profile" : {
"mark" : "newmark",
"category" : "general",
//other 60 fields
}
}
Expected Result
{
"_id" : ObjectId("63a00087347cec20308de2ec"),
"registration" : {
"email" : "admin@admin.com",
"password" : "password"
},
"profile" : {
"name" : "My Name",
"mark" : "newmark",
"category" : "general",
//other 60 fields
}
}
Thank You