using MongoDB.Driver; using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.Models; using System; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Update { public partial class MongoDbUpdater : DataAccessBase { public MongoDbUpdater(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } /// /// Asynchronously Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document with the modifications you want to persist. public virtual async Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", modifiedDocument.Id); var updateRes = await HandlePartitioned(modifiedDocument).ReplaceOneAsync(filter, modifiedDocument); return updateRes.ModifiedCount == 1; } /// /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document with the modifications you want to persist. public virtual bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", modifiedDocument.Id); var updateRes = HandlePartitioned(modifiedDocument).ReplaceOne(filter, modifiedDocument); return updateRes.ModifiedCount == 1; } /// /// Takes a document you want to modify and applies the update you have defined in MongoDb. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to modify. /// The update definition for the document. public virtual async Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); var updateRes = await HandlePartitioned(documentToModify).UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true }); return updateRes.ModifiedCount == 1; } /// /// Takes a document you want to modify and applies the update you have defined in MongoDb. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to modify. /// The update definition for the document. public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); var updateRes = HandlePartitioned(documentToModify).UpdateOne(filter, update, new UpdateOptions { IsUpsert = true }); return updateRes.ModifiedCount == 1; } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document you want to modify. /// The field selector. /// The new value of the property field. public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); var updateRes = await HandlePartitioned(documentToModify).UpdateOneAsync(filter, Builders.Update.Set(field, value)); return updateRes.ModifiedCount == 1; } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document you want to modify. /// The field selector. /// The new value of the property field. public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); var updateRes = HandlePartitioned(documentToModify).UpdateOne(filter, Builders.Update.Set(field, value)); return updateRes.ModifiedCount == 1; } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The value of the partition key. public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); var updateRes = await collection.UpdateOneAsync(filter, Builders.Update.Set(field, value)); return updateRes.ModifiedCount == 1; } /// /// For the entity selected by the filter, updates the property field with the given value. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The partition key for the document. public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return await UpdateOneAsync(Builders.Filter.Where(filter), field, value, partitionKey); } /// /// Updates the property field with the given value. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The value of the partition key. public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); var updateRes = collection.UpdateOne(filter, Builders.Update.Set(field, value)); return updateRes.ModifiedCount == 1; } /// /// For the entity selected by the filter, updates the property field with the given value. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The partition key for the document. public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return UpdateOne(Builders.Filter.Where(filter), field, value, partitionKey); } /// /// For the entities selected by the filter, updates the property field with the given value. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The partition key for the document. public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return await UpdateManyAsync(Builders.Filter.Where(filter), field, value, partitionKey); } /// /// For the entities selected by the filter, updates the property field with the given value. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The value of the partition key. public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); var updateRes = await collection.UpdateManyAsync(filter, Builders.Update.Set(field, value)); return updateRes.ModifiedCount; } /// /// For the entities selected by the filter, apply the update definition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document filter. /// The field selector. /// The new value of the property field. /// The value of the partition key. public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition update, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return await UpdateManyAsync(Builders.Filter.Where(filter), update, partitionKey); } /// /// For the entities selected by the filter, apply the update definition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The update definition. /// The value of the partition key. public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); var updateRes = await collection.UpdateManyAsync(filter, updateDefinition, new UpdateOptions { IsUpsert = true }); return updateRes.ModifiedCount; } /// /// For the entities selected by the filter, updates the property field with the given value. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The partition key for the document. public virtual long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return UpdateMany(Builders.Filter.Where(filter), field, value, partitionKey); } /// /// For the entities selected by the filter, updates the property field with the given value. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The value of the partition key. public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); var updateRes = collection.UpdateMany(filter, Builders.Update.Set(field, value)); return updateRes.ModifiedCount; } /// /// For the entities selected by the filter, apply the update definition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The update definition. /// The value of the partition key. public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition UpdateDefinition, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); var updateRes = collection.UpdateMany(filter, UpdateDefinition, new UpdateOptions { IsUpsert = true }); return updateRes.ModifiedCount; } } }