using MongoDB.Driver; using MongoDbGenericRepository.Models; using System; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Update { public partial class MongoDbUpdater { /// /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The client session. /// The document with the modifications you want to persist. /// The optional cancellation token. /// public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) 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; } /// /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The client session. /// The document with the modifications you want to persist. /// The optional cancellation token. /// public virtual bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) 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; } /// /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The client session. /// The document to modify. /// The update definition. /// The optional cancellation token. /// public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) 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; } /// /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The client session. /// The document to modify. /// The update definition. /// The optional cancellation token. /// public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) 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; } /// /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field to update. /// The client session. /// The document to modify. /// The field to update. /// The value of the field. /// The optional cancellation token. /// public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) 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; } /// /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field to update. /// The client session. /// The document to modify. /// The field to update. /// The value of the field. /// The optional cancellation token. /// public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) 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; } /// /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field to update. /// The client session. /// The filter for the update. /// The field to update. /// The value of the field. /// The optional partition key. /// The optional cancellation token. /// public virtual async Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) 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; } /// /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field to update. /// The client session. /// The filter for the update. /// The field to update. /// The value of the field. /// The optional partition key. /// The optional cancellation token. /// public virtual Task UpdateOneAsync(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable { return UpdateOneAsync(session, Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); } /// /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field to update. /// The client session. /// The filter for the update. /// The field to update. /// The value of the field. /// The optional partition key. /// The optional cancellation token. /// public virtual bool UpdateOne(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) 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; } /// /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field to update. /// The client session. /// The filter for the update. /// The field to update. /// The value of the field. /// The optional partition key. /// The optional cancellation token. /// public virtual bool UpdateOne(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable { return UpdateOne(session, Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); } } }