I'm wondering if there exists a $setIfNullOrEmpty operator. I'm currently using an upsert like this:
const filter = {
id: 123
}
const updates = {
$set: {
varx: valx
},
$inc: { vary: valy },
$setOnInsert: z
};
const options = { upsert: true };
collection.updateOneWithOptions(filter, updates, options);
I would like to also have an option to $set some value if in the database it's null or an empty string. My ideal updates object would look like this (does something like this exist?):
const updates = {
$set: {
varx: valx
},
$setIfNullOrEmpty: {
varxy: varxy
}
$inc: { vary: valy },
$setOnInsert: z
};
I understand that I could make 2 queries (1 to grab the item I'm looking for, check for that property and another to update the item) but I'm looking for a way to make 1 query. Is there a way to do this?