Over on my MongoDB C# Tutorial Richard asked how you would prevent multiple users from updating the same document. In this post I’m going to update the project in the tutorial to support this.
In my blog example this issue could occur when user 1 clicks to edit a post. While editing, user 2 also edits the same post. User 2 finishes their edit before user 1 and saves the post. User 1 continues editing and saves their changes. User 2’s changes are completely overwritten and neither user has any idea that it happened.
In this implementation, based on the above scenario I’m going to display an error to user 2 to tell them somebody else has updated to the post. They won’t be able to save their changes and it would be up to them to reload the post and make their changes again. Of course you could do something much more exciting, like show the two versions side by side and allow the user to merge the changes.