Inconsistent behavior with the UpdateAsync and UpdateOne methods in C# MongoDB.Driver

Intermittent Update Failure with FindOneAndReplaceAsync in MongoDB .NET Driver

I’m working on a project using .NET 9 with MongoDB.Bson 3.2.1 and MongoDB.Driver 3.2.1. I’ve implemented a method to update a document in MongoDB using FindOneAndReplaceAsync, and I’m running into an issue where the update doesn’t always persist in the database, even though the method returns the updated document. Here’s the code I’m using:
public virtual async Task ReplaceOneAndReturnNewVersionAsync(TDocument document)
{
var filter = Builders.Filter.Eq(doc => doc.Id, document.Id);
return await _collection.FindOneAndReplaceAsync(filter, document, new FindOneAndReplaceOptions { ReturnDocument = ReturnDocument.After });
}
(Note: I’ve set the ReturnDocument.After option to return the updated document, as implied by the method name.)

The Problem

Most of the time, this works fine—the method returns the updated document as expected. However, about 1 out of 10 times (sometimes 1 out of 4 or 5), when I check the database afterward, the old version of the document is still there. This behavior is random and inconsistent, making it hard to pinpoint the cause.

Any advice on how to troubleshoot or resolve this would be greatly appreciated. Thank you!