Added delete functionality
This commit is contained in:
@@ -0,0 +1,129 @@
|
|||||||
|
using IntegrationTests.Infrastructure;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace IntegrationTests
|
||||||
|
{
|
||||||
|
public class DeleteTestsPartitionedDocument : PartitionedDocument
|
||||||
|
{
|
||||||
|
public DeleteTestsPartitionedDocument() : base("TestPartitionKey")
|
||||||
|
{
|
||||||
|
Version = 1;
|
||||||
|
}
|
||||||
|
public string SomeContent { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DeletePartitionedTests : BaseMongoDbRepositoryTests<DeleteTestsPartitionedDocument>
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void PartitionedDeleteOne()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne(document);
|
||||||
|
// Act
|
||||||
|
var result = SUT.DeleteOne(document);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(1, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void PartitionedDeleteOneLinq()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne(document);
|
||||||
|
// Act
|
||||||
|
var result = SUT.DeleteOne<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(1, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task PartitionedDeleteOneAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne(document);
|
||||||
|
// Act
|
||||||
|
var result = await SUT.DeleteOneAsync(document);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(1, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task PartitionedDeleteOneAsyncLinq()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne(document);
|
||||||
|
// Act
|
||||||
|
var result = await SUT.DeleteOneAsync<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(1, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task PartitionedDeleteManyAsyncLinq()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||||
|
SUT.AddMany(documents);
|
||||||
|
// Act
|
||||||
|
var result = await SUT.DeleteManyAsync<DeleteTestsPartitionedDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(5, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task PartitionedDeleteManyAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||||
|
SUT.AddMany(documents);
|
||||||
|
// Act
|
||||||
|
var result = await SUT.DeleteManyAsync(documents);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(5, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void PartitionedDeleteManyLinq()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var content = "DeleteManyLinqContent";
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
documents.ForEach(e => e.SomeContent = content);
|
||||||
|
SUT.AddMany(documents);
|
||||||
|
// Act
|
||||||
|
var result = SUT.DeleteMany<DeleteTestsPartitionedDocument>(e => e.SomeContent == content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(5, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.SomeContent == content, PartitionKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void PartitionedDeleteMany()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var content = "DeleteManyContent";
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
documents.ForEach(e => e.SomeContent = content);
|
||||||
|
SUT.AddMany(documents);
|
||||||
|
// Act
|
||||||
|
var result = SUT.DeleteMany(documents);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(5, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.SomeContent == content, PartitionKey));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
using IntegrationTests.Infrastructure;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace IntegrationTests
|
||||||
|
{
|
||||||
|
public class DeleteTestsDocument : Document
|
||||||
|
{
|
||||||
|
public DeleteTestsDocument()
|
||||||
|
{
|
||||||
|
Version = 2;
|
||||||
|
}
|
||||||
|
public string SomeContent { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DeleteTests : BaseMongoDbRepositoryTests<DeleteTestsDocument>
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void DeleteOne()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne(document);
|
||||||
|
// Act
|
||||||
|
var result = SUT.DeleteOne(document);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(1, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.Id == document.Id));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteOneLinq()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne(document);
|
||||||
|
// Act
|
||||||
|
var result = SUT.DeleteOne<DeleteTestsDocument>(e => e.Id == document.Id);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(1, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.Id == document.Id));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task DeleteOneAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne(document);
|
||||||
|
// Act
|
||||||
|
var result = await SUT.DeleteOneAsync(document);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(1, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.Id == document.Id));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task DeleteOneAsyncLinq()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne(document);
|
||||||
|
// Act
|
||||||
|
var result = await SUT.DeleteOneAsync<DeleteTestsDocument>(e => e.Id == document.Id);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(1, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.Id == document.Id));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task DeleteManyAsyncLinq()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||||
|
SUT.AddMany(documents);
|
||||||
|
// Act
|
||||||
|
var result = await SUT.DeleteManyAsync<DeleteTestsDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent");
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(5, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task DeleteManyAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||||
|
SUT.AddMany(documents);
|
||||||
|
// Act
|
||||||
|
var result = await SUT.DeleteManyAsync(documents);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(5, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteManyLinq()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var content = "DeleteManyLinqContent";
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
documents.ForEach(e => e.SomeContent = content);
|
||||||
|
SUT.AddMany(documents);
|
||||||
|
// Act
|
||||||
|
var result = SUT.DeleteMany<DeleteTestsDocument>(e => e.SomeContent == content);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(5, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.SomeContent == content));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteMany()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var content = "DeleteManyContent";
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
documents.ForEach(e => e.SomeContent = content);
|
||||||
|
SUT.AddMany(documents);
|
||||||
|
// Act
|
||||||
|
var result = SUT.DeleteMany(documents);
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(5, result);
|
||||||
|
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.SomeContent == content));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -56,6 +56,8 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="DeletePartitionedTests.cs" />
|
||||||
|
<Compile Include="DeleteTests.cs" />
|
||||||
<Compile Include="Infrastructure\BaseMongoDbRepositoryTests.cs" />
|
<Compile Include="Infrastructure\BaseMongoDbRepositoryTests.cs" />
|
||||||
<Compile Include="CreatePartitionedTests.cs" />
|
<Compile Include="CreatePartitionedTests.cs" />
|
||||||
<Compile Include="Infrastructure\ITestRepository.cs" />
|
<Compile Include="Infrastructure\ITestRepository.cs" />
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ namespace IntegrationTests
|
|||||||
public class UpdatePartitionedTests : BaseMongoDbRepositoryTests<UpdateTestsPartitionedDocument>
|
public class UpdatePartitionedTests : BaseMongoDbRepositoryTests<UpdateTestsPartitionedDocument>
|
||||||
{
|
{
|
||||||
[Test]
|
[Test]
|
||||||
public void UpdateOne()
|
public void PartitionedUpdateOne()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var document = CreateTestDocument();
|
var document = CreateTestDocument();
|
||||||
@@ -33,7 +33,7 @@ namespace IntegrationTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task UpdateOneAsync()
|
public async Task PartitionedUpdateOneAsync()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var document = CreateTestDocument();
|
var document = CreateTestDocument();
|
||||||
|
|||||||
@@ -155,7 +155,77 @@ namespace MongoDbGenericRepository
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Delete
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="TDocument">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
Task<long> DeleteOneAsync<TDocument>(TDocument document) where TDocument : IDocument;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="TDocument">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
long DeleteOne<TDocument>(TDocument document) where TDocument : IDocument;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a list of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a list of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
long DeleteMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -373,141 +443,7 @@ namespace MongoDbGenericRepository
|
|||||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).Count();
|
return HandlePartitioned<TDocument>(partitionKey).Find(filter).Count();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
#endregion
|
||||||
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
|
|
||||||
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public async Task<List<TDocument>> GetPaginatedAsync<TDocument>(Expression<Func<TDocument, bool>> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a list of projected objects
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">T is a DbEntity</typeparam>
|
|
||||||
/// <typeparam name="TProjection"></typeparam>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<TProjection> ProjectBy<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
where TProjection : class, new()
|
|
||||||
{
|
|
||||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter)
|
|
||||||
.Project(projection)
|
|
||||||
.FirstOrDefaultAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion Get
|
|
||||||
|
|
||||||
#region Delete
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A generic delete one method
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
|
||||||
/// <param name="id"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<long> DeleteOneAsync<TDocument>(TDocument document) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await DeleteOneAsync<TDocument>(x => x.Id == document.Id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A generic delete one method
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
|
||||||
/// <param name="id"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public long DeleteOne<TDocument>(TDocument document) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return DeleteOne<TDocument>(x => x.Id == document.Id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A generic delete one method
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return GetCollection<TDocument>().DeleteOne(filter).DeletedCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A generic delete one method
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return (await GetCollection<TDocument>().DeleteOneAsync(filter)).DeletedCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A generic delete many method
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
var deleteRes = await GetCollection<TDocument>().DeleteManyAsync(filter);
|
|
||||||
return deleteRes.DeletedCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A generic delete many method
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
if (!documents.Any())
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
var idsTodelete = documents.Select(e => e.Id).ToArray();
|
|
||||||
var deleteRes = await GetCollection<TDocument>().DeleteManyAsync(x => idsTodelete.Contains(x.Id));
|
|
||||||
return deleteRes.DeletedCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A generic delete many method
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public long DeleteMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
if (!documents.Any())
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
var idsTodelete = documents.Select(e => e.Id).ToArray();
|
|
||||||
var deleteRes = GetCollection<TDocument>().DeleteMany(x => idsTodelete.Contains(x.Id));
|
|
||||||
return deleteRes.DeletedCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A generic delete many method
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
var deleteRes = GetCollection<TDocument>().DeleteMany(filter);
|
|
||||||
return deleteRes.DeletedCount;
|
|
||||||
}
|
|
||||||
#endregion Delete
|
|
||||||
|
|
||||||
#region Update
|
#region Update
|
||||||
|
|
||||||
@@ -535,6 +471,147 @@ namespace MongoDbGenericRepository
|
|||||||
|
|
||||||
#endregion Update
|
#endregion Update
|
||||||
|
|
||||||
|
#region Delete
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="TDocument">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public async Task<long> DeleteOneAsync<TDocument>(TDocument document) where TDocument : IDocument
|
||||||
|
{
|
||||||
|
return (await HandlePartitioned(document).DeleteOneAsync(x => x.Id == document.Id)).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="TDocument">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public long DeleteOne<TDocument>(TDocument document) where TDocument : IDocument
|
||||||
|
{
|
||||||
|
return HandlePartitioned(document).DeleteOne(x => x.Id == document.Id).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument>(partitionKey).DeleteOne(filter).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||||
|
{
|
||||||
|
return (await HandlePartitioned<TDocument>(partitionKey).DeleteOneAsync(filter)).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||||
|
{
|
||||||
|
return (await HandlePartitioned<TDocument>(partitionKey).DeleteManyAsync(filter)).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a list of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
|
||||||
|
{
|
||||||
|
if (!documents.Any())
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
var idsTodelete = documents.Select(e => e.Id).ToArray();
|
||||||
|
return (await HandlePartitioned(documents.FirstOrDefault()).DeleteManyAsync(x => idsTodelete.Contains(x.Id))).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a list of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public long DeleteMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
|
||||||
|
{
|
||||||
|
if (!documents.Any())
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
var idsTodelete = documents.Select(e => e.Id).ToArray();
|
||||||
|
return HandlePartitioned(documents.FirstOrDefault()).DeleteMany(x => idsTodelete.Contains(x.Id)).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument>(partitionKey).DeleteMany(filter).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Delete
|
||||||
|
|
||||||
|
#region Projection
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected document matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <typeparam name="TProjection"></typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public async Task<TProjection> GetProjectedAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument
|
||||||
|
where TProjection : class, new()
|
||||||
|
{
|
||||||
|
return await HandlePartitioned<TDocument>(partitionKey).Find(filter)
|
||||||
|
.Project(projection)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
|
||||||
|
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public async Task<List<TDocument>> GetPaginatedAsync<TDocument>(Expression<Func<TDocument, bool>> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) where TDocument : IDocument
|
||||||
|
{
|
||||||
|
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
#region Find And Update
|
#region Find And Update
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -552,22 +629,13 @@ namespace MongoDbGenericRepository
|
|||||||
|
|
||||||
#endregion Find And Update
|
#endregion Find And Update
|
||||||
|
|
||||||
/// <summary>
|
#region Private Methods
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
|
||||||
/// <param name="partitionKey"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument
|
private IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument
|
||||||
{
|
{
|
||||||
return _mongoDbContext.GetCollection<TDocument>(partitionKey);
|
return _mongoDbContext.GetCollection<TDocument>(partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The private GetCollection method
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
|
||||||
/// <returns></returns>
|
|
||||||
private IMongoCollection<TDocument> GetCollection<TDocument>() where TDocument : IDocument
|
private IMongoCollection<TDocument> GetCollection<TDocument>() where TDocument : IDocument
|
||||||
{
|
{
|
||||||
return _mongoDbContext.GetCollection<TDocument>();
|
return _mongoDbContext.GetCollection<TDocument>();
|
||||||
@@ -607,5 +675,7 @@ namespace MongoDbGenericRepository
|
|||||||
document.AddedAtUtc = DateTime.UtcNow;
|
document.AddedAtUtc = DateTime.UtcNow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user