using System; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Update { public partial class MongoDbUpdater { /// public virtual async Task UpdateOneAsync( IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", modifiedDocument.Id); var updateRes = await HandlePartitioned(modifiedDocument) .ReplaceOneAsync(session, filter, modifiedDocument, cancellationToken: cancellationToken) .ConfigureAwait(false); return updateRes.ModifiedCount == 1; } /// public virtual bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", modifiedDocument.Id); var updateRes = HandlePartitioned(modifiedDocument) .ReplaceOne(session, filter, modifiedDocument, cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } /// public virtual async Task UpdateOneAsync( IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); var updateRes = await HandlePartitioned(documentToModify).UpdateOneAsync(session, filter, update, null, cancellationToken) .ConfigureAwait(false); return updateRes.ModifiedCount == 1; } /// public virtual bool UpdateOne( IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); var updateRes = HandlePartitioned(documentToModify).UpdateOne(session, filter, update, null, cancellationToken); return updateRes.ModifiedCount == 1; } /// public virtual async Task UpdateOneAsync( IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); var updateRes = await HandlePartitioned(documentToModify) .UpdateOneAsync(session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken) .ConfigureAwait(false); return updateRes.ModifiedCount == 1; } /// public virtual bool UpdateOne( IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); var updateRes = HandlePartitioned(documentToModify).UpdateOne( session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } /// public virtual async Task UpdateOneAsync( IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); var updateRes = await collection.UpdateOneAsync(session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken) .ConfigureAwait(false); return updateRes.ModifiedCount == 1; } /// public virtual Task UpdateOneAsync( IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return UpdateOneAsync(session, Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); } /// public virtual bool UpdateOne( IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); var updateRes = collection.UpdateOne(session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } /// public virtual bool UpdateOne( IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return UpdateOne(session, Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); } } }