Fix Retrieve updated document in ReplaceOneAsync method
This commit is contained in:
parent
a97e4e2219
commit
dbc21959eb
@ -104,11 +104,13 @@ namespace Core.Blueprint.Mongo
|
||||
void ReplaceOne(TDocument document);
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously replaces an existing document with a new one.
|
||||
/// Asynchronously replaces an existing document in the collection and returns the updated version.
|
||||
/// </summary>
|
||||
/// <param name="document">The document to replace the existing one.</param>
|
||||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
||||
Task ReplaceOneAsync(TDocument document);
|
||||
/// <param name="document">The document with the updated data. Its _Id is used to locate the existing document.</param>
|
||||
/// <returns>
|
||||
/// The updated document if the replacement was successful; otherwise, <c>null</c> if no matching document was found.
|
||||
/// </returns>
|
||||
Task<TDocument?> ReplaceOneAsync(TDocument document);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a single document by the provided filter expression.
|
||||
|
||||
@ -175,16 +175,27 @@ namespace Core.Blueprint.Mongo
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously replaces an existing document in the collection.
|
||||
/// Asynchronously replaces an existing document in the collection and returns the updated version.
|
||||
/// </summary>
|
||||
/// <param name="document">The document with the updated data.</param>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
public virtual async Task ReplaceOneAsync(TDocument document)
|
||||
/// <param name="document">The document with the updated data. Its _Id is used to locate the existing document.</param>
|
||||
/// <returns>
|
||||
/// The updated document if the replacement was successful; otherwise, <c>null</c> if no matching document was found.
|
||||
/// </returns>
|
||||
public virtual async Task<TDocument?> ReplaceOneAsync(TDocument document)
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq(doc => doc._Id, document._Id);
|
||||
await _collection.FindOneAndReplaceAsync(filter, document);
|
||||
|
||||
var options = new FindOneAndReplaceOptions<TDocument>
|
||||
{
|
||||
ReturnDocument = ReturnDocument.After // return the updated document
|
||||
};
|
||||
|
||||
var result = await _collection.FindOneAndReplaceAsync(filter, document, options);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a single document from the collection based on the provided filter expression.
|
||||
/// </summary>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user