Hey friends! 👋🏻
I wanted to share a cool backend fix I implemented while adding the comments editing feature to my microforum, Axion. It's about handling no-op updates cleanly without breaking error handling.
In the past, some of my editing routes would update the content unconditionally. Even if the old and new content were exactly the same, the database would still run the update, hit the disk, and change the timestamps. It was inefficient honestly.
And letting MongoDB update the timestamps on identical content breaks data integrity. It lies to you—saying the last update happened at X time when nothing actually changed. Plus, it would return modifiedCount === 1 just because a timestamp field got changed, which ruins the tracking of actual content changes.
When adding comment updates, I wanted to avoid this behavior entirely.
The Naive Approach. I could have simply used { content: { $ne: content } }. But this creates a blindspot in my logic. If the database returns 0 matched documents, you can't differentiate between two completely different scenarios:
- Does the document actually exist and belong to the logged-in user?
- Did the query just fail because the new content matches the old content?
To give a proper response to the client, I needed to know the difference.
My Solution: Aggregation Pipeline Updates. Instead of a basic update, I relied on an aggregation pipeline inside the update operation. And that's my logic:
- It first finds a document created by the current logged-in user where the
_idmatches the given comment ID. - If found, it updates the content and timestamps only if the old content differs from the new content.
- Finally, it checks
matchedCount === 0. If it's 0, we know that the document wasn't found (or the user doesn't own it) and can throw a proper 404 error here.
Oh, yeah, and to end this cleanly, I included a changed field in the HTTP response, setting its value to modifiedCount !== 0. This tells the frontend exactly whether an actual database write occurred or if it was a harmless no-op.
Handling the "(Edited)" Badge Properly. While doing this, I also needed a way for the frontend to show an "(Edited)" badge on updated comment cards.
I didn't want to rely on a single boolean like isEdited: true because it doesn't provide the metadata of when the update happened if I ever need it. I also rejected the idea of just comparing createdAt against updatedAt. Like, what if a background script or admin tool ever modifies a document for maintenance? updatedAt changes, and the system would falsely tell users the comment was edited when it wasn't.
So, I came up with a cleaner approach: adding a third field called editedAt. For non-edited comments, it stays null. The moment a real edit happens, it stores the exact timestamp of that edit. Maybe I don't show the edit timestamp to users right now, but if I ever want to in the future, the data will be there. It's a matter of handling "what if" scenarios cleanly from day one.
At the end of the day, losing precise error handling isn't worth saving a few lines of code.
Thanks for hanging out and reading through! How do you handle these annoying no-op updates and edit tracking in your backend? Let's talk in the comments below. See you next time! 🌟