Hello,
I’m wondering if there’s a way to return custom errors and/or messages from queries depending on the data in the dataset.
For example and clarification, consider that I have the following collection, named users, with data in this format:
{
"_id": "65506e0b9ab7d8f1f9e23373",
"first_name": "John",
"last_name": "Smith",
"age": 33,
"occupation": "Software engineer",
"is_active": false,
}
And I would like to change the occupation field if the _id field matches AND the is_active field is true, however, I would also like to know the reason why the document wasn’t updated if it wasn’t.
One way to do this of course is by fetching the document that matches the _id field then validate (using programming language code) if the field is_active == true and if it is, then use an update query to update the occupation field, but it seems wasteful to me to make two queries for a simple update.
The logic I’m looking for in a SINGLE query is something similar to:
if there is a document with _id that matches then:
if document.is_active == false:
return CUSTOM_INACTIVE_USER_ERROR
else:
update document.occupation to the new value
the CUSTOM_INACTIVE_USER_ERROR can be a JSON document for example:
{
"error": "in_active is set to false, can't update"
}
Thank you in advance.