In my current setup, I am utilizing mongoose to write data to a MongoDB collection while ensuring there are no null fields. Default values have been set in the document for this purpose. During an update function call, certain fields may be null but I do not want these null fields to trigger any modifications.
For instance:
const Business = require("./businessModel") //Referring to the model
const {id, email, name, contactNumber} = args
const business = await Business.findByIdAndUpdate(
{ id},
{
name: ((name != null) ? name : (skip this field))... //HERE
});
At the marked section, if the 'name' field is not null and contains a value, then update it with the new input value. Otherwise, leave it unchanged and skip this field modification. I do have an alternative method where I fetch the document first and then replace it with the default value from the schema, but I consider this approach suboptimal due to the additional document call it requires.