188 lines
11 KiB
C#
188 lines
11 KiB
C#
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)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously Updates a document.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument modifiedDocument)
|
|
where TDocument : IDocument<TKey>
|
|
where TKey : IEquatable<TKey>
|
|
{
|
|
var filter = Builders<TDocument>.Filter.Eq("Id", modifiedDocument.Id);
|
|
var updateRes = await HandlePartitioned<TDocument, TKey>(modifiedDocument).ReplaceOneAsync(filter, modifiedDocument);
|
|
return updateRes.ModifiedCount == 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a document.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
|
public virtual bool UpdateOne<TDocument, TKey>(TDocument modifiedDocument)
|
|
where TDocument : IDocument<TKey>
|
|
where TKey : IEquatable<TKey>
|
|
{
|
|
var filter = Builders<TDocument>.Filter.Eq("Id", modifiedDocument.Id);
|
|
var updateRes = HandlePartitioned<TDocument, TKey>(modifiedDocument).ReplaceOne(filter, modifiedDocument);
|
|
return updateRes.ModifiedCount == 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Takes a document you want to modify and applies the update you have defined in MongoDb.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
/// <param name="documentToModify">The document you want to modify.</param>
|
|
/// <param name="update">The update definition for the document.</param>
|
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
|
where TDocument : IDocument<TKey>
|
|
where TKey : IEquatable<TKey>
|
|
{
|
|
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
|
var updateRes = await HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true });
|
|
return updateRes.ModifiedCount == 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Takes a document you want to modify and applies the update you have defined in MongoDb.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
/// <param name="documentToModify">The document you want to modify.</param>
|
|
/// <param name="update">The update definition for the document.</param>
|
|
public virtual bool UpdateOne<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
|
where TDocument : IDocument<TKey>
|
|
where TKey : IEquatable<TKey>
|
|
{
|
|
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
|
var updateRes = HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOne(filter, update, new UpdateOptions { IsUpsert = true });
|
|
return updateRes.ModifiedCount == 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the property field with the given value update a property field in entities.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
/// <typeparam name="TField">The type of the field.</typeparam>
|
|
/// <param name="documentToModify">The document you want to modify.</param>
|
|
/// <param name="field">The field selector.</param>
|
|
/// <param name="value">The new value of the property field.</param>
|
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
|
where TDocument : IDocument<TKey>
|
|
where TKey : IEquatable<TKey>
|
|
{
|
|
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
|
var updateRes = await HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOneAsync(filter, Builders<TDocument>.Update.Set(field, value));
|
|
return updateRes.ModifiedCount == 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the property field with the given value update a property field in entities.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
/// <typeparam name="TField">The type of the field.</typeparam>
|
|
/// <param name="documentToModify">The document you want to modify.</param>
|
|
/// <param name="field">The field selector.</param>
|
|
/// <param name="value">The new value of the property field.</param>
|
|
public virtual bool UpdateOne<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
|
where TDocument : IDocument<TKey>
|
|
where TKey : IEquatable<TKey>
|
|
{
|
|
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
|
var updateRes = HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOne(filter, Builders<TDocument>.Update.Set(field, value));
|
|
return updateRes.ModifiedCount == 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the property field with the given value update a property field in entities.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
/// <typeparam name="TField">The type of the field.</typeparam>
|
|
/// <param name="filter">The document filter.</param>
|
|
/// <param name="field">The field selector.</param>
|
|
/// <param name="value">The new value of the property field.</param>
|
|
/// <param name="partitionKey">The value of the partition key.</param>
|
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
|
where TDocument : IDocument<TKey>
|
|
where TKey : IEquatable<TKey>
|
|
{
|
|
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
|
|
var updateRes = await collection.UpdateOneAsync(filter, Builders<TDocument>.Update.Set(field, value));
|
|
return updateRes.ModifiedCount == 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// For the entity selected by the filter, updates the property field with the given value.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
/// <typeparam name="TField">The type of the field.</typeparam>
|
|
/// <param name="filter">The document filter.</param>
|
|
/// <param name="field">The field selector.</param>
|
|
/// <param name="value">The new value of the property field.</param>
|
|
/// <param name="partitionKey">The partition key for the document.</param>
|
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
|
where TDocument : IDocument<TKey>
|
|
where TKey : IEquatable<TKey>
|
|
{
|
|
return await UpdateOneAsync<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the property field with the given value.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
/// <typeparam name="TField">The type of the field.</typeparam>
|
|
/// <param name="filter">The document filter.</param>
|
|
/// <param name="field">The field selector.</param>
|
|
/// <param name="value">The new value of the property field.</param>
|
|
/// <param name="partitionKey">The value of the partition key.</param>
|
|
public virtual bool UpdateOne<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
|
where TDocument : IDocument<TKey>
|
|
where TKey : IEquatable<TKey>
|
|
{
|
|
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
|
|
var updateRes = collection.UpdateOne(filter, Builders<TDocument>.Update.Set(field, value));
|
|
return updateRes.ModifiedCount == 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// For the entity selected by the filter, updates the property field with the given value.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
/// <typeparam name="TField">The type of the field.</typeparam>
|
|
/// <param name="filter">The document filter.</param>
|
|
/// <param name="field">The field selector.</param>
|
|
/// <param name="value">The new value of the property field.</param>
|
|
/// <param name="partitionKey">The partition key for the document.</param>
|
|
public virtual bool UpdateOne<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
|
where TDocument : IDocument<TKey>
|
|
where TKey : IEquatable<TKey>
|
|
{
|
|
return UpdateOne<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
|
|
}
|
|
}
|
|
}
|