using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDbGenericRepository.DataAccess.Base;
using MongoDbGenericRepository.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace MongoDbGenericRepository.DataAccess.Delete
{
public class MongoDbEraser : DataAccessBase
{
public MongoDbEraser(IMongoDbContext mongoDbContext) : base(mongoDbContext)
{
}
#region Delete TKey
///
/// Deletes a document.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The document you want to delete.
/// The number of documents deleted.
public virtual long DeleteOne(TDocument document)
where TDocument : IDocument
where TKey : IEquatable
{
var filter = Builders.Filter.Eq("Id", document.Id);
return HandlePartitioned(document).DeleteOne(filter).DeletedCount;
}
///
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The document you want to delete.
/// The number of documents deleted.
public virtual async Task DeleteOneAsync(TDocument document)
where TDocument : IDocument
where TKey : IEquatable
{
var filter = Builders.Filter.Eq("Id", document.Id);
return (await HandlePartitioned(document).DeleteOneAsync(filter)).DeletedCount;
}
///
/// Deletes a document matching the condition of the LINQ expression filter.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// A LINQ expression filter.
/// An optional partition key.
/// The number of documents deleted.
public virtual long DeleteOne(Expression> filter, string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
{
return HandlePartitioned(partitionKey).DeleteOne(filter).DeletedCount;
}
///
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// A LINQ expression filter.
/// An optional partition key.
/// The number of documents deleted.
public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
{
return (await HandlePartitioned(partitionKey).DeleteOneAsync(filter)).DeletedCount;
}
///
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// A LINQ expression filter.
/// An optional partition key.
/// The number of documents deleted.
public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
{
return (await HandlePartitioned(partitionKey).DeleteManyAsync(filter)).DeletedCount;
}
///
/// Asynchronously deletes a list of documents.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The list of documents to delete.
/// The number of documents deleted.
public virtual async Task DeleteManyAsync(IEnumerable documents)
where TDocument : IDocument
where TKey : IEquatable
{
if (!documents.Any())
{
return 0;
}
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
if (documents.Any(e => e is IPartitionedDocument))
{
long deleteCount = 0;
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
{
var groupIdsTodelete = group.Select(e => e.Id).ToArray();
deleteCount += (await HandlePartitioned(group.FirstOrDefault()).DeleteManyAsync(x => groupIdsTodelete.Contains(x.Id))).DeletedCount;
}
return deleteCount;
}
else
{
var idsTodelete = documents.Select(e => e.Id).ToArray();
return (await HandlePartitioned(documents.FirstOrDefault()).DeleteManyAsync(x => idsTodelete.Contains(x.Id))).DeletedCount;
}
}
///
/// Deletes a list of documents.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The list of documents to delete.
/// The number of documents deleted.
public virtual long DeleteMany(IEnumerable documents)
where TDocument : IDocument
where TKey : IEquatable
{
if (!documents.Any())
{
return 0;
}
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
if (documents.Any(e => e is IPartitionedDocument))
{
long deleteCount = 0;
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
{
var groupIdsTodelete = group.Select(e => e.Id).ToArray();
deleteCount += (HandlePartitioned(group.FirstOrDefault()).DeleteMany(x => groupIdsTodelete.Contains(x.Id))).DeletedCount;
}
return deleteCount;
}
else
{
var idsTodelete = documents.Select(e => e.Id).ToArray();
return (HandlePartitioned(documents.FirstOrDefault()).DeleteMany(x => idsTodelete.Contains(x.Id))).DeletedCount;
}
}
///
/// Deletes the documents matching the condition of the LINQ expression filter.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// A LINQ expression filter.
/// An optional partition key.
/// The number of documents deleted.
public virtual long DeleteMany(Expression> filter, string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
{
return HandlePartitioned(partitionKey).DeleteMany(filter).DeletedCount;
}
#endregion
}
}