I am currently using mongoose to write data to a document in a MongoDB collection but I accept no null fields, I already have set default values in the document. I call an update function that has some fields as null, those fields that are null already, I do not want them to make a modification.
Example:
const Business = require("./businessModel") //This references the model
const {id, email, name, contactNumber} = args
const business = await Business.findByIdAndUpdate(
{ id},
{
name: ((name != null) ? name : (skip this field))... //HERE
});
Where I commented here, if the name is not null, which means a value exists for it then have the predefined schema value name now set to the new name input, otherwise don't change anything and just skip the field. I do already have an alternative where I call the document first then replace it with the default value for the document but that requires a document call which I do not believe to be an optimal solution.