I have a document in this form
{
"firstName": "John",
"lastName": "Doe",
"preferences": {
"xxx": true,
"yyy": false,
"zzz": true
}
}
I want to update specific fields in the preferences
embedded document. If I use the $set
operand in the following way
db.users.update({name: "John"}, {$set: {preferences: {"xxx": false}}})
the preferences
document is completely replaced, so I get this
{
"firstName": "John",
"lastName": "Doe",
"preferences": {
"xxx": false
}
}
while I would like to obtain this
{
"firstName": "John",
"lastName": "Doe",
"preferences": {
"xxx": false,
"yyy": false,
"zzz": true
}
}
I cannot use:
db.users.update({name: "John"}, {$set: {"preferences.xxx": false}})
because I don't know which specific field I'm going to update, I only have as input an object that may or may not contain each field of the preferences
document.