I have a file containing sub-categories
{
a: fire,
b: {
plane: fly,
c: swim,
d: jump,
}
}
During upserts, I want to specifically change the "plane" category.
Here is what I have attempted:
const { categories, ...rest } = newRecord;
const updateCategories = categories;
delete updateCategories?.plane;
await Model.findOneAndUpdate(
{
id: newRecord.id,
},
{
$set: {
...rest,
categories: updateCategories,
},
$setOnInsert: {
"categories.plane": newRecord.plane,
},
},
{
upsert: true,
});
The error message I keep encountering is: Unable to update both categories and categories simultaneously. Even though I am removing the plane entry from the data being updated, referencing it through the categories field seems to cause confusion.
How can I rephrase this so that mongo understands my intention to only impact the "plane" sub-category within the categories, not the entire categories field in the $setOnInsert? Is there a way to indicate that an insert operation is taking place in the $set block for adding a conditional statement?
EDIT: It should be noted that I am utilizing documentDb on aws, which is believed to be built on mongo 4.0.