Assuming I have the following document in MongoDB:
{
_id: 1,
answer: 42,
parent: {
lock: true
}
}
After running
updateOne({_id: 1}, {$unset: {'parent.lock': ""}});
The document becomes:
{
_id: 1,
answer: 42,
parent: {}
}
Is there a way to have the 'parent' field also be removed only if the result of the update comment is an empty object other than making a second call?
In other words, I want the original doc to become:
{
_id: 1,
answer: 42
}
However, if the embedded document of the parent field also had other fields (was originally):
{
_id: 1,
answer: 42,
parent: {
lock: true,
child: 'other'
}
}
Then the result of the update command would be:
{
_id: 1,
answer: 42,
parent: {
child: 'other'
}
}