Compare commits
57 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 592c30ed2c | |||
| 83f1d2c7a6 | |||
| ab294d1a5d | |||
| dd8cbe3da7 | |||
| 0844c51566 | |||
| 601c0fd02d | |||
| 91d15312e6 | |||
| 54b2a63a8e | |||
| dbb27aad39 | |||
| afd0308849 | |||
| 9cb35c5881 | |||
| c5390214eb | |||
| ac1fd709a6 | |||
| f6b0aa274b | |||
| 7a82a281fb | |||
| dca01b184c | |||
| b1cda2ac50 | |||
| 8d6b796e7f | |||
| 1a7763a312 | |||
| 77cc85574c | |||
| 846bb16730 | |||
| 5e4cbfa1b3 | |||
| 804dc18c4f | |||
| 182ee0a335 | |||
| bd662d25d6 | |||
| 6c8d4bdafb | |||
| 4d94a1b436 | |||
| d91a644b4a | |||
| 9d1950e9c7 | |||
| 67d0a4c8a3 | |||
| 1826668cea | |||
| b855677efd | |||
| 530309e9fc | |||
| 69ad94e4a3 | |||
| baaf2b5ee9 | |||
| d2df667b3f | |||
| 8ccced66ea | |||
| c92a126ecc | |||
| 2207f2f1a1 | |||
| 0626292d09 | |||
| 29f01d2dab | |||
| a7391e06f8 | |||
| 43677b72a1 | |||
| 2f29efafe3 | |||
| d2f465b063 | |||
| 75b894cb2a | |||
| ba723be738 | |||
| f773e599d0 | |||
| 17653b05f7 | |||
| 69903fd69b | |||
| 9edd7e97fe | |||
| b77978e57a | |||
| 648b57612d | |||
| 4ac0ffe91c | |||
| eaa90d67f5 | |||
| 2d866d966c | |||
| ffc7e65b7b |
@@ -1,6 +1,6 @@
|
|||||||
using CoreIntegrationTests.Infrastructure;
|
using CoreIntegrationTests.Infrastructure;
|
||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using System;
|
|
||||||
namespace CoreIntegrationTests
|
namespace CoreIntegrationTests
|
||||||
{
|
{
|
||||||
public class CoreObjectIdTestDocument : TestDoc<ObjectId>
|
public class CoreObjectIdTestDocument : TestDoc<ObjectId>
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
using CoreIntegrationTests.Infrastructure;
|
using CoreIntegrationTests.Infrastructure;
|
||||||
|
using MongoDB.Bson;
|
||||||
using MongoDbGenericRepository.Attributes;
|
using MongoDbGenericRepository.Attributes;
|
||||||
using MongoDbGenericRepository.Models;
|
using MongoDbGenericRepository.Models;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace CoreIntegrationTests
|
namespace CoreIntegrationTests
|
||||||
{
|
{
|
||||||
|
#region Guid Type
|
||||||
|
|
||||||
[CollectionName("TestingCNameAttrPartTKey")]
|
[CollectionName("TestingCNameAttrPartTKey")]
|
||||||
public class CoreTKeyPartitionedCollectionNameDoc : TestDoc<Guid>, IPartitionedDocument
|
public class CoreTKeyPartitionedCollectionNameDoc : TestDoc<Guid>, IPartitionedDocument
|
||||||
{
|
{
|
||||||
@@ -28,4 +31,35 @@ namespace CoreIntegrationTests
|
|||||||
return "CoreCRUDTKeyPartitionedCollectionNameAttributeTests";
|
return "CoreCRUDTKeyPartitionedCollectionNameAttributeTests";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion Guid Type
|
||||||
|
|
||||||
|
|
||||||
|
#region ObjectId Type
|
||||||
|
|
||||||
|
[CollectionName("TestingCNameAttrPartObjectId")]
|
||||||
|
public class CoreObjectIdPartitionedCollectionNameDoc : TestDoc<ObjectId>, IPartitionedDocument
|
||||||
|
{
|
||||||
|
public CoreObjectIdPartitionedCollectionNameDoc()
|
||||||
|
{
|
||||||
|
PartitionKey = "CoreTestPartitionKeyObjectId";
|
||||||
|
}
|
||||||
|
|
||||||
|
public string PartitionKey { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CRUDObjectIdPartitionedCollectionNameAttributeTests : MongoDbTKeyDocumentTestBase<CoreObjectIdPartitionedCollectionNameDoc, ObjectId>
|
||||||
|
{
|
||||||
|
public CRUDObjectIdPartitionedCollectionNameAttributeTests(MongoDbTestFixture<CoreObjectIdPartitionedCollectionNameDoc, ObjectId> fixture) : base(fixture)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string GetClassName()
|
||||||
|
{
|
||||||
|
return "CoreCRUDTKeyPartitionedCollectionNameAttributeTests";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion ObjectId Type
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ namespace CoreIntegrationTests
|
|||||||
{
|
{
|
||||||
public CRUDTests(MongoDbTestFixture<CoreTestDocument, Guid> fixture) : base(fixture)
|
public CRUDTests(MongoDbTestFixture<CoreTestDocument, Guid> fixture) : base(fixture)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string GetClassName()
|
public override string GetClassName()
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||||
@@ -7,11 +7,13 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.1.2" />
|
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.1.2" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.2" />
|
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.2" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
|
<PackageReference Include="MongoDB.Driver" Version="2.9.3" />
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.7.0" />
|
<PackageReference Include="MongoDbGenericRepository" Version="1.4.4" />
|
||||||
<PackageReference Include="MongoDbGenericRepository" Version="1.3.8" />
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
<PackageReference Include="xunit" Version="2.4.0" />
|
<PackageReference Include="xunit.runner.console" Version="2.4.1">
|
||||||
<PackageReference Include="xunit.runner.console" Version="2.4.0" />
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" />
|
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
+62
-165
@@ -1,4 +1,5 @@
|
|||||||
using MongoDbGenericRepository.Models;
|
using MongoDbGenericRepository;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
@@ -11,7 +12,7 @@ using Xunit;
|
|||||||
|
|
||||||
namespace CoreIntegrationTests.Infrastructure
|
namespace CoreIntegrationTests.Infrastructure
|
||||||
{
|
{
|
||||||
public abstract class MongoDbDocumentTestBase<T> :
|
public abstract partial class MongoDbDocumentTestBase<T> :
|
||||||
IClassFixture<MongoDbTestFixture<T, Guid>>
|
IClassFixture<MongoDbTestFixture<T, Guid>>
|
||||||
where T : TestDoc, new()
|
where T : TestDoc, new()
|
||||||
{
|
{
|
||||||
@@ -30,7 +31,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
_fixture.PartitionKey = PartitionKey;
|
_fixture.PartitionKey = PartitionKey;
|
||||||
TestClassName = GetClassName();
|
TestClassName = GetClassName();
|
||||||
MongoDbConfig.EnsureConfigured();
|
MongoDbConfig.EnsureConfigured();
|
||||||
SUT = TestRepository.Instance;
|
SUT = TestTKeyRepository<Guid>.Instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected T CreateTestDocument()
|
protected T CreateTestDocument()
|
||||||
@@ -63,7 +64,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// SUT: System Under Test
|
/// SUT: System Under Test
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected static ITestRepository SUT { get; set; }
|
protected static ITestRepository<Guid> SUT { get; set; }
|
||||||
|
|
||||||
#region Add
|
#region Add
|
||||||
|
|
||||||
@@ -71,7 +72,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
public void AddOne()
|
public void AddOne()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var document = new T();
|
var document = CreateTestDocument();
|
||||||
// Act
|
// Act
|
||||||
SUT.AddOne<T>(document);
|
SUT.AddOne<T>(document);
|
||||||
// Assert
|
// Assert
|
||||||
@@ -84,7 +85,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
public async Task AddOneAsync()
|
public async Task AddOneAsync()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var document = new T();
|
var document = CreateTestDocument();
|
||||||
// Act
|
// Act
|
||||||
await SUT.AddOneAsync<T>(document);
|
await SUT.AddOneAsync<T>(document);
|
||||||
// Assert
|
// Assert
|
||||||
@@ -97,7 +98,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
public void AddMany()
|
public void AddMany()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var documents = new List<T> { new T(), new T() };
|
var documents = CreateTestDocuments(2);
|
||||||
// Act
|
// Act
|
||||||
SUT.AddMany<T>(documents);
|
SUT.AddMany<T>(documents);
|
||||||
// Assert
|
// Assert
|
||||||
@@ -115,7 +116,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
if (!string.IsNullOrEmpty(PartitionKey))
|
if (!string.IsNullOrEmpty(PartitionKey))
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var documents = new List<T> { new T(), new T(), new T(), new T() };
|
var documents = CreateTestDocuments(4);
|
||||||
if (documents.Any(e => e is IPartitionedDocument))
|
if (documents.Any(e => e is IPartitionedDocument))
|
||||||
{
|
{
|
||||||
var secondPartitionKey = $"{PartitionKey}-2";
|
var secondPartitionKey = $"{PartitionKey}-2";
|
||||||
@@ -138,7 +139,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
public async Task AddManyAsync()
|
public async Task AddManyAsync()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var documents = new List<T> { new T(), new T() };
|
var documents = CreateTestDocuments(2);
|
||||||
// Act
|
// Act
|
||||||
await SUT.AddManyAsync<T>(documents);
|
await SUT.AddManyAsync<T>(documents);
|
||||||
// Assert
|
// Assert
|
||||||
@@ -156,7 +157,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
if (!string.IsNullOrEmpty(PartitionKey))
|
if (!string.IsNullOrEmpty(PartitionKey))
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var documents = new List<T> { new T(), new T(), new T(), new T() };
|
var documents = CreateTestDocuments(4);
|
||||||
if (documents.Any(e => e is IPartitionedDocument))
|
if (documents.Any(e => e is IPartitionedDocument))
|
||||||
{
|
{
|
||||||
var secondPartitionKey = $"{PartitionKey}-2";
|
var secondPartitionKey = $"{PartitionKey}-2";
|
||||||
@@ -235,7 +236,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
SUT.AddOne<T>(document);
|
SUT.AddOne<T>(document);
|
||||||
// Act
|
// Act
|
||||||
var cursor = SUT.GetCursor<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
var cursor = SUT.GetCursor<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||||
var count = cursor.Count();
|
var count = cursor.CountDocuments();
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True(1 == count, GetTestName());
|
Assert.True(1 == count, GetTestName());
|
||||||
}
|
}
|
||||||
@@ -346,160 +347,6 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
|
|
||||||
#endregion Read
|
#endregion Read
|
||||||
|
|
||||||
#region Update
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void UpdateOne()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T>(document);
|
|
||||||
var content = GetContent();
|
|
||||||
document.SomeContent = content;
|
|
||||||
// Act
|
|
||||||
var result = SUT.UpdateOne<T>(document);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result);
|
|
||||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument, GetTestName());
|
|
||||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task UpdateOneAsync()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T>(document);
|
|
||||||
var content = GetContent();
|
|
||||||
document.SomeContent = content;
|
|
||||||
// Act
|
|
||||||
var result = await SUT.UpdateOneAsync<T>(document);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result);
|
|
||||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument, GetTestName());
|
|
||||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void UpdateOneField()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T>(document);
|
|
||||||
var content = GetContent();
|
|
||||||
// Act
|
|
||||||
var result = SUT.UpdateOne<T, string>(document, x => x.SomeContent, content);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result, GetTestName());
|
|
||||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument, GetTestName());
|
|
||||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task UpdateOneFieldAsync()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T>(document);
|
|
||||||
var content = GetContent();
|
|
||||||
// Act
|
|
||||||
var result = await SUT.UpdateOneAsync<T, string>(document, x => x.SomeContent, content);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result, GetTestName());
|
|
||||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument, GetTestName());
|
|
||||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void UpdateOneFieldWithFilter()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T>(document);
|
|
||||||
var content = GetContent();
|
|
||||||
// Act
|
|
||||||
var result = SUT.UpdateOne<T, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result, GetTestName());
|
|
||||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument, GetTestName());
|
|
||||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task UpdateOneFieldWithFilterAsync()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T>(document);
|
|
||||||
var content = GetContent();
|
|
||||||
// Act
|
|
||||||
var result = await SUT.UpdateOneAsync<T, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result, GetTestName());
|
|
||||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument, GetTestName());
|
|
||||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task UpdateOneAsyncWithUpdateDefinition()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T>(document);
|
|
||||||
var childrenToAdd = new List<Child>
|
|
||||||
{
|
|
||||||
new Child("testType1", "testValue1"),
|
|
||||||
new Child("testType2", "testValue2")
|
|
||||||
};
|
|
||||||
|
|
||||||
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var result = await SUT.UpdateOneAsync<T>(document, updateDef);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result);
|
|
||||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument);
|
|
||||||
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
|
||||||
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
|
||||||
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
|
||||||
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void UpdateOneWithUpdateDefinition()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T>(document);
|
|
||||||
var childrenToAdd = new List<Child>
|
|
||||||
{
|
|
||||||
new Child("testType1", "testValue1"),
|
|
||||||
new Child("testType2", "testValue2")
|
|
||||||
};
|
|
||||||
|
|
||||||
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var result = SUT.UpdateOne<T>(document, updateDef);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result);
|
|
||||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument);
|
|
||||||
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
|
||||||
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
|
||||||
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
|
||||||
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion Update
|
|
||||||
|
|
||||||
#region Delete
|
#region Delete
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -1075,6 +922,56 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
|
|
||||||
#endregion Index Management
|
#endregion Index Management
|
||||||
|
|
||||||
|
#region Math
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SumByDecimalAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
var i = 1;
|
||||||
|
documents.ForEach(e =>
|
||||||
|
{
|
||||||
|
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||||
|
e.Nested.SomeAmount = 5m;
|
||||||
|
e.SomeContent = criteria;
|
||||||
|
});
|
||||||
|
SUT.AddMany<T>(documents);
|
||||||
|
var expectedSum = documents.Sum(e => e.Nested.SomeAmount);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await SUT.SumByAsync<T>(e => e.SomeContent == criteria, e => e.Nested.SomeAmount, PartitionKey);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expectedSum, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SumByDecimal()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
var i = 1;
|
||||||
|
documents.ForEach(e =>
|
||||||
|
{
|
||||||
|
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||||
|
e.Nested.SomeAmount = 5m;
|
||||||
|
e.SomeContent = criteria;
|
||||||
|
});
|
||||||
|
SUT.AddMany<T>(documents);
|
||||||
|
var expectedSum = documents.Sum(e => e.Nested.SomeAmount);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = SUT.SumBy<T>(e => e.SomeContent == criteria, e => e.Nested.SomeAmount, PartitionKey);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expectedSum, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Math
|
||||||
|
|
||||||
#region Test Utils
|
#region Test Utils
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||||
@@ -0,0 +1,381 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CoreIntegrationTests.Infrastructure
|
||||||
|
{
|
||||||
|
public abstract partial class MongoDbDocumentTestBase<T> :
|
||||||
|
IClassFixture<MongoDbTestFixture<T, Guid>>
|
||||||
|
where T : TestDoc, new()
|
||||||
|
{
|
||||||
|
#region Update One
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateOne()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T>(document);
|
||||||
|
var content = GetContent();
|
||||||
|
document.SomeContent = content;
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateOne<T>(document);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result);
|
||||||
|
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument, GetTestName());
|
||||||
|
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateOneAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T>(document);
|
||||||
|
var content = GetContent();
|
||||||
|
document.SomeContent = content;
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateOneAsync<T>(document);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result);
|
||||||
|
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument, GetTestName());
|
||||||
|
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateOneField()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T>(document);
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateOne<T, string>(document, x => x.SomeContent, content);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument, GetTestName());
|
||||||
|
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateOneFieldAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T>(document);
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateOneAsync<T, string>(document, x => x.SomeContent, content);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument, GetTestName());
|
||||||
|
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateOneFieldWithFilter()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T>(document);
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateOne<T, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument, GetTestName());
|
||||||
|
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateOneFieldWithFilterAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T>(document);
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateOneAsync<T, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument, GetTestName());
|
||||||
|
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateOneAsyncWithUpdateDefinition()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T>(document);
|
||||||
|
var childrenToAdd = new List<Child>
|
||||||
|
{
|
||||||
|
new Child("testType1", "testValue1"),
|
||||||
|
new Child("testType2", "testValue2")
|
||||||
|
};
|
||||||
|
|
||||||
|
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateOneAsync<T>(document, updateDef);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result);
|
||||||
|
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument);
|
||||||
|
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateOneWithUpdateDefinition()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T>(document);
|
||||||
|
var childrenToAdd = new List<Child>
|
||||||
|
{
|
||||||
|
new Child("testType1", "testValue1"),
|
||||||
|
new Child("testType2", "testValue2")
|
||||||
|
};
|
||||||
|
|
||||||
|
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateOne<T>(document, updateDef);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result);
|
||||||
|
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument);
|
||||||
|
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Update One
|
||||||
|
|
||||||
|
#region Update Many
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateManyWithLinqFilterAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T>(documents);
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateManyAsync<T, string>(x => docIds.Contains(x.Id), x => x.SomeContent, content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetAll<T>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocument.Count == 2);
|
||||||
|
Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateManyWithFilterDefinitionAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T>(documents);
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var filterDefinition = Builders<T>.Filter.Where(x => docIds.Contains(x.Id));
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateManyAsync<T, string>(filterDefinition, x => x.SomeContent, content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetAll<T>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocument.Count == 2);
|
||||||
|
Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateManyWithLinqFilterAndUpdateDefinitionAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T>(documents);
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var childrenToAdd = new List<Child>
|
||||||
|
{
|
||||||
|
new Child("testType1", "testValue1"),
|
||||||
|
new Child("testType2", "testValue2")
|
||||||
|
};
|
||||||
|
|
||||||
|
var updateDef = Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateManyAsync<T>(x => docIds.Contains(x.Id), updateDef, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocuments = SUT.GetAll<T>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocuments.Count == 2);
|
||||||
|
updatedDocuments.ForEach(updatedDocument =>
|
||||||
|
{
|
||||||
|
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateManyWithFilterAndUpdateDefinitionsAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T>(documents);
|
||||||
|
var childrenToAdd = new List<Child>
|
||||||
|
{
|
||||||
|
new Child("testType1", "testValue1"),
|
||||||
|
new Child("testType2", "testValue2")
|
||||||
|
};
|
||||||
|
|
||||||
|
var updateDef = Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||||
|
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var filterDefinition = Builders<T>.Filter.Where(x => docIds.Contains(x.Id));
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateManyAsync<T>(filterDefinition, updateDef, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocuments = SUT.GetAll<T>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocuments.Count == 2);
|
||||||
|
updatedDocuments.ForEach(updatedDocument =>
|
||||||
|
{
|
||||||
|
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateManyWithLinqFilter()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T>(documents);
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateMany<T, string>(x => docIds.Contains(x.Id), x => x.SomeContent, content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetAll<T>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
Assert.True(updatedDocument.Count == 2);
|
||||||
|
Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateManyWithFilterDefinition()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T>(documents);
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var filterDefinition = Builders<T>.Filter.Where(x => docIds.Contains(x.Id));
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateMany<T, string>(filterDefinition, x => x.SomeContent, content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetAll<T>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocument.Count == 2);
|
||||||
|
Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateManyWithLinqFilterAndUpdateDefinition()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T>(documents);
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var childrenToAdd = new List<Child>
|
||||||
|
{
|
||||||
|
new Child("testType1", "testValue1"),
|
||||||
|
new Child("testType2", "testValue2")
|
||||||
|
};
|
||||||
|
|
||||||
|
var updateDef = Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateMany<T>(x => docIds.Contains(x.Id), updateDef, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocuments = SUT.GetAll<T>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocuments.Count == 2);
|
||||||
|
updatedDocuments.ForEach(updatedDocument =>
|
||||||
|
{
|
||||||
|
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateManyWithFilterAndUpdateDefinitions()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T>(documents);
|
||||||
|
var childrenToAdd = new List<Child>
|
||||||
|
{
|
||||||
|
new Child("testType1", "testValue1"),
|
||||||
|
new Child("testType2", "testValue2")
|
||||||
|
};
|
||||||
|
|
||||||
|
var updateDef = Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||||
|
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var filterDefinition = Builders<T>.Filter.Where(x => docIds.Contains(x.Id));
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateMany<T>(filterDefinition, updateDef, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocuments = SUT.GetAll<T>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocuments.Count == 2);
|
||||||
|
updatedDocuments.ForEach(updatedDocument =>
|
||||||
|
{
|
||||||
|
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion Update Many
|
||||||
|
}
|
||||||
|
}
|
||||||
+259
-184
@@ -1,4 +1,5 @@
|
|||||||
using MongoDbGenericRepository.Models;
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
@@ -11,11 +12,11 @@ using Xunit;
|
|||||||
|
|
||||||
namespace CoreIntegrationTests.Infrastructure
|
namespace CoreIntegrationTests.Infrastructure
|
||||||
{
|
{
|
||||||
public abstract class MongoDbTKeyDocumentTestBase<T, TKey> :
|
public abstract partial class MongoDbTKeyDocumentTestBase<T, TKey> :
|
||||||
IClassFixture<MongoDbTestFixture<T, TKey>>
|
IClassFixture<MongoDbTestFixture<T, TKey>>
|
||||||
where T: TestDoc<TKey>, new()
|
where T : TestDoc<TKey>, new()
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
|
|
||||||
{
|
{
|
||||||
private readonly MongoDbTestFixture<T, TKey> _fixture;
|
private readonly MongoDbTestFixture<T, TKey> _fixture;
|
||||||
|
|
||||||
@@ -72,11 +73,11 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
public void AddOne()
|
public void AddOne()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var document = new T();
|
var document = CreateTestDocument();
|
||||||
// Act
|
// Act
|
||||||
SUT.AddOne<T, TKey>(document);
|
SUT.AddOne<T, TKey>(document);
|
||||||
// Assert
|
// Assert
|
||||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(document.Id))
|
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(document.Id))
|
||||||
: SUT.Count<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
: SUT.Count<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||||
Assert.True(1 == count, GetTestName());
|
Assert.True(1 == count, GetTestName());
|
||||||
}
|
}
|
||||||
@@ -85,20 +86,20 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
public async Task AddOneAsync()
|
public async Task AddOneAsync()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var document = new T();
|
var document = CreateTestDocument();
|
||||||
// Act
|
// Act
|
||||||
await SUT.AddOneAsync<T, TKey>(document);
|
await SUT.AddOneAsync<T, TKey>(document);
|
||||||
// Assert
|
// Assert
|
||||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(document.Id))
|
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(document.Id))
|
||||||
: SUT.Count<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
: SUT.Count<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||||
Assert.True (1 == count, GetTestName());
|
Assert.True(1 == count, GetTestName());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void AddMany()
|
public void AddMany()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var documents = new List<T> { new T(), new T() };
|
var documents = CreateTestDocuments(2);
|
||||||
// Act
|
// Act
|
||||||
SUT.AddMany<T, TKey>(documents);
|
SUT.AddMany<T, TKey>(documents);
|
||||||
// Assert
|
// Assert
|
||||||
@@ -106,7 +107,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
|| e.Id.Equals(documents[1].Id))
|
|| e.Id.Equals(documents[1].Id))
|
||||||
: SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
: SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
||||||
|| e.Id.Equals(documents[1].Id), PartitionKey);
|
|| e.Id.Equals(documents[1].Id), PartitionKey);
|
||||||
Assert.True (2 == count, GetTestName());
|
Assert.True(2 == count, GetTestName());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -116,7 +117,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
if (!string.IsNullOrEmpty(PartitionKey))
|
if (!string.IsNullOrEmpty(PartitionKey))
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var documents = new List<T> { new T(), new T(), new T(), new T() };
|
var documents = CreateTestDocuments(4);
|
||||||
if (documents.Any(e => e is IPartitionedDocument))
|
if (documents.Any(e => e is IPartitionedDocument))
|
||||||
{
|
{
|
||||||
var secondPartitionKey = $"{PartitionKey}-2";
|
var secondPartitionKey = $"{PartitionKey}-2";
|
||||||
@@ -139,7 +140,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
public async Task AddManyAsync()
|
public async Task AddManyAsync()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var documents = new List<T> { new T(), new T() };
|
var documents = CreateTestDocuments(2);
|
||||||
// Act
|
// Act
|
||||||
await SUT.AddManyAsync<T, TKey>(documents);
|
await SUT.AddManyAsync<T, TKey>(documents);
|
||||||
// Assert
|
// Assert
|
||||||
@@ -147,7 +148,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
|| e.Id.Equals(documents[1].Id))
|
|| e.Id.Equals(documents[1].Id))
|
||||||
: SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
: SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
||||||
|| e.Id.Equals(documents[1].Id), PartitionKey);
|
|| e.Id.Equals(documents[1].Id), PartitionKey);
|
||||||
Assert.True (2 == count, GetTestName());
|
Assert.True(2 == count, GetTestName());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -157,7 +158,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
if (!string.IsNullOrEmpty(PartitionKey))
|
if (!string.IsNullOrEmpty(PartitionKey))
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var documents = new List<T> { new T(), new T(), new T(), new T() };
|
var documents = CreateTestDocuments(4);
|
||||||
if (documents.Any(e => e is IPartitionedDocument))
|
if (documents.Any(e => e is IPartitionedDocument))
|
||||||
{
|
{
|
||||||
var secondPartitionKey = $"{PartitionKey}-2";
|
var secondPartitionKey = $"{PartitionKey}-2";
|
||||||
@@ -236,9 +237,9 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
SUT.AddOne<T, TKey>(document);
|
SUT.AddOne<T, TKey>(document);
|
||||||
// Act
|
// Act
|
||||||
var cursor = SUT.GetCursor<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
var cursor = SUT.GetCursor<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||||
var count = cursor.Count();
|
var count = cursor.CountDocuments();
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True (1 == count, GetTestName());
|
Assert.True(1 == count, GetTestName());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -347,160 +348,6 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
|
|
||||||
#endregion Read
|
#endregion Read
|
||||||
|
|
||||||
#region Update
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void UpdateOne()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T, TKey>(document);
|
|
||||||
var content = GetContent();
|
|
||||||
document.SomeContent = content;
|
|
||||||
// Act
|
|
||||||
var result = SUT.UpdateOne<T, TKey>(document);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result);
|
|
||||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument, GetTestName());
|
|
||||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task UpdateOneAsync()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T, TKey>(document);
|
|
||||||
var content = GetContent();
|
|
||||||
document.SomeContent = content;
|
|
||||||
// Act
|
|
||||||
var result = await SUT.UpdateOneAsync<T, TKey>(document);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result);
|
|
||||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument, GetTestName());
|
|
||||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void UpdateOneField()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T, TKey>(document);
|
|
||||||
var content = GetContent();
|
|
||||||
// Act
|
|
||||||
var result = SUT.UpdateOne<T, TKey, string>(document, x => x.SomeContent, content);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result, GetTestName());
|
|
||||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument, GetTestName());
|
|
||||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task UpdateOneFieldAsync()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T, TKey>(document);
|
|
||||||
var content = GetContent();
|
|
||||||
// Act
|
|
||||||
var result = await SUT.UpdateOneAsync<T, TKey, string>(document, x => x.SomeContent, content);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result, GetTestName());
|
|
||||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument, GetTestName());
|
|
||||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void UpdateOneFieldWithFilter()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T, TKey>(document);
|
|
||||||
var content = GetContent();
|
|
||||||
// Act
|
|
||||||
var result = SUT.UpdateOne<T, TKey, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result, GetTestName());
|
|
||||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument, GetTestName());
|
|
||||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task UpdateOneFieldWithFilterAsync()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T, TKey>(document);
|
|
||||||
var content = GetContent();
|
|
||||||
// Act
|
|
||||||
var result = await SUT.UpdateOneAsync<T, TKey, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result, GetTestName());
|
|
||||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument, GetTestName());
|
|
||||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task UpdateOneAsyncWithUpdateDefinition()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T, TKey>(document);
|
|
||||||
var childrenToAdd = new List<Child>
|
|
||||||
{
|
|
||||||
new Child("testType1", "testValue1"),
|
|
||||||
new Child("testType2", "testValue2")
|
|
||||||
};
|
|
||||||
|
|
||||||
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var result = await SUT.UpdateOneAsync<T, TKey>(document, updateDef);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result);
|
|
||||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument);
|
|
||||||
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
|
||||||
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
|
||||||
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
|
||||||
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void UpdateOneWithUpdateDefinition()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var document = CreateTestDocument();
|
|
||||||
SUT.AddOne<T, TKey>(document);
|
|
||||||
var childrenToAdd = new List<Child>
|
|
||||||
{
|
|
||||||
new Child("testType1", "testValue1"),
|
|
||||||
new Child("testType2", "testValue2")
|
|
||||||
};
|
|
||||||
|
|
||||||
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var result = SUT.UpdateOne<T, TKey>(document, updateDef);
|
|
||||||
// Assert
|
|
||||||
Assert.True(result);
|
|
||||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
|
||||||
Assert.True(null != updatedDocument);
|
|
||||||
Assert.True(childrenToAdd[0].Type== updatedDocument.Children[0].Type, GetTestName());
|
|
||||||
Assert.True(childrenToAdd[0].Value== updatedDocument.Children[0].Value, GetTestName());
|
|
||||||
Assert.True(childrenToAdd[1].Type== updatedDocument.Children[1].Type, GetTestName());
|
|
||||||
Assert.True(childrenToAdd[1].Value== updatedDocument.Children[1].Value, GetTestName());
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion Update
|
|
||||||
|
|
||||||
#region Delete
|
#region Delete
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -512,7 +359,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
// Act
|
// Act
|
||||||
var result = SUT.DeleteOne<T, TKey>(document);
|
var result = SUT.DeleteOne<T, TKey>(document);
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True (1 == result);
|
Assert.True(1 == result);
|
||||||
Assert.False(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
Assert.False(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -525,7 +372,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
// Act
|
// Act
|
||||||
var result = SUT.DeleteOne<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
var result = SUT.DeleteOne<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True (1 == result);
|
Assert.True(1 == result);
|
||||||
Assert.False(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
Assert.False(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -538,7 +385,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
// Act
|
// Act
|
||||||
var result = await SUT.DeleteOneAsync<T, TKey>(document);
|
var result = await SUT.DeleteOneAsync<T, TKey>(document);
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True (1 == result);
|
Assert.True(1 == result);
|
||||||
Assert.False(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
Assert.False(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -551,7 +398,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
// Act
|
// Act
|
||||||
var result = await SUT.DeleteOneAsync<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
var result = await SUT.DeleteOneAsync<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True (1 == result);
|
Assert.True(1 == result);
|
||||||
Assert.False(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
Assert.False(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -768,7 +615,8 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
||||||
var documents = CreateTestDocuments(5);
|
var documents = CreateTestDocuments(5);
|
||||||
var i = 1;
|
var i = 1;
|
||||||
documents.ForEach(e => {
|
documents.ForEach(e =>
|
||||||
|
{
|
||||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||||
e.SomeContent = criteria;
|
e.SomeContent = criteria;
|
||||||
});
|
});
|
||||||
@@ -790,7 +638,8 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
||||||
var documents = CreateTestDocuments(5);
|
var documents = CreateTestDocuments(5);
|
||||||
var i = 1;
|
var i = 1;
|
||||||
documents.ForEach(e => {
|
documents.ForEach(e =>
|
||||||
|
{
|
||||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||||
e.SomeContent = criteria;
|
e.SomeContent = criteria;
|
||||||
});
|
});
|
||||||
@@ -812,7 +661,8 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
||||||
var documents = CreateTestDocuments(5);
|
var documents = CreateTestDocuments(5);
|
||||||
var i = 1;
|
var i = 1;
|
||||||
documents.ForEach(e => {
|
documents.ForEach(e =>
|
||||||
|
{
|
||||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||||
e.SomeContent = criteria;
|
e.SomeContent = criteria;
|
||||||
});
|
});
|
||||||
@@ -834,7 +684,8 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
||||||
var documents = CreateTestDocuments(5);
|
var documents = CreateTestDocuments(5);
|
||||||
var i = 1;
|
var i = 1;
|
||||||
documents.ForEach(e => {
|
documents.ForEach(e =>
|
||||||
|
{
|
||||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||||
e.SomeContent = criteria;
|
e.SomeContent = criteria;
|
||||||
});
|
});
|
||||||
@@ -856,7 +707,8 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
||||||
var documents = CreateTestDocuments(5);
|
var documents = CreateTestDocuments(5);
|
||||||
var i = 1;
|
var i = 1;
|
||||||
documents.ForEach(e => {
|
documents.ForEach(e =>
|
||||||
|
{
|
||||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||||
e.SomeContent = criteria;
|
e.SomeContent = criteria;
|
||||||
});
|
});
|
||||||
@@ -878,7 +730,8 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
||||||
var documents = CreateTestDocuments(5);
|
var documents = CreateTestDocuments(5);
|
||||||
var i = 1;
|
var i = 1;
|
||||||
documents.ForEach(e => {
|
documents.ForEach(e =>
|
||||||
|
{
|
||||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||||
e.SomeContent = criteria;
|
e.SomeContent = criteria;
|
||||||
});
|
});
|
||||||
@@ -900,7 +753,8 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
||||||
var documents = CreateTestDocuments(5);
|
var documents = CreateTestDocuments(5);
|
||||||
var i = 1;
|
var i = 1;
|
||||||
documents.ForEach(e => {
|
documents.ForEach(e =>
|
||||||
|
{
|
||||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||||
e.SomeContent = criteria;
|
e.SomeContent = criteria;
|
||||||
});
|
});
|
||||||
@@ -908,7 +762,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First();
|
var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = SUT.GetMinValue< T, TKey, DateTime >(e => e.SomeContent == criteria, e => e.Nested.SomeDate, PartitionKey);
|
var result = SUT.GetMinValue<T, TKey, DateTime>(e => e.SomeContent == criteria, e => e.Nested.SomeDate, PartitionKey);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True(result != default(DateTime));
|
Assert.True(result != default(DateTime));
|
||||||
@@ -922,7 +776,8 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
||||||
var documents = CreateTestDocuments(5);
|
var documents = CreateTestDocuments(5);
|
||||||
var i = 1;
|
var i = 1;
|
||||||
documents.ForEach(e => {
|
documents.ForEach(e =>
|
||||||
|
{
|
||||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||||
e.SomeContent = criteria;
|
e.SomeContent = criteria;
|
||||||
});
|
});
|
||||||
@@ -930,7 +785,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First();
|
var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await SUT.GetMinValueAsync< T, TKey, DateTime >(e => e.SomeContent == criteria, e => e.Nested.SomeDate, PartitionKey);
|
var result = await SUT.GetMinValueAsync<T, TKey, DateTime>(e => e.SomeContent == criteria, e => e.Nested.SomeDate, PartitionKey);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True(result != default(DateTime));
|
Assert.True(result != default(DateTime));
|
||||||
@@ -1056,6 +911,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
// Act
|
// Act
|
||||||
Expression<Func<T, object>> ex = x => x.SomeContent4;
|
Expression<Func<T, object>> ex = x => x.SomeContent4;
|
||||||
Expression<Func<T, object>> ex2 = x => x.SomeContent5;
|
Expression<Func<T, object>> ex2 = x => x.SomeContent5;
|
||||||
|
|
||||||
var result = await SUT.CreateCombinedTextIndexAsync<T, TKey>(new[] { ex, ex2 }, null, PartitionKey);
|
var result = await SUT.CreateCombinedTextIndexAsync<T, TKey>(new[] { ex, ex2 }, null, PartitionKey);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
@@ -1068,7 +924,226 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
|
|
||||||
#endregion Index Management
|
#endregion Index Management
|
||||||
|
|
||||||
|
#region Math
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SumByDecimalAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
var i = 1;
|
||||||
|
documents.ForEach(e =>
|
||||||
|
{
|
||||||
|
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||||
|
e.Nested.SomeAmount = 5m;
|
||||||
|
e.SomeContent = criteria;
|
||||||
|
});
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
var expectedSum = documents.Sum(e => e.Nested.SomeAmount);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await SUT.SumByAsync<T, TKey>(e => e.SomeContent == criteria, e => e.Nested.SomeAmount, PartitionKey);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expectedSum, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SumByDecimal()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
var i = 1;
|
||||||
|
documents.ForEach(e =>
|
||||||
|
{
|
||||||
|
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||||
|
e.Nested.SomeAmount = 5m;
|
||||||
|
e.SomeContent = criteria;
|
||||||
|
});
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
var expectedSum = documents.Sum(e => e.Nested.SomeAmount);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = SUT.SumBy<T, TKey>(e => e.SomeContent == criteria, e => e.Nested.SomeAmount, PartitionKey);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expectedSum, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Math
|
||||||
|
|
||||||
|
#region Group By
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GroupByTProjection()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
var content = GetContent();
|
||||||
|
for (var i = 0; i < documents.Count - 2; i++)
|
||||||
|
{
|
||||||
|
documents[i].GroupingKey = 1;
|
||||||
|
documents[i].SomeContent = $"{content}-{i}";
|
||||||
|
}
|
||||||
|
for (var i = 3; i < documents.Count; i++)
|
||||||
|
{
|
||||||
|
documents[i].GroupingKey = 2;
|
||||||
|
documents[i].SomeContent = $"{content}-{i}";
|
||||||
|
}
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = SUT.GroupBy<T, int, ProjectedGroup, TKey>(
|
||||||
|
e => e.GroupingKey,
|
||||||
|
g => new ProjectedGroup
|
||||||
|
{
|
||||||
|
Key = g.Key,
|
||||||
|
Content = g.Select(doc => doc.SomeContent).ToList()
|
||||||
|
},
|
||||||
|
PartitionKey);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var key1Group = result.First(e => e.Key == 1);
|
||||||
|
Assert.NotNull(key1Group);
|
||||||
|
Assert.Equal(3, key1Group.Content.Count);
|
||||||
|
var key2Group = result.First(e => e.Key == 2);
|
||||||
|
Assert.NotNull(key2Group);
|
||||||
|
Assert.Equal(2, key2Group.Content.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void FilteredGroupByTProjection()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(5);
|
||||||
|
var content = GetContent();
|
||||||
|
for (var i = 0; i < documents.Count - 2; i++)
|
||||||
|
{
|
||||||
|
documents[i].GroupingKey = 4;
|
||||||
|
documents[i].SomeContent = $"{content}-{i}";
|
||||||
|
}
|
||||||
|
for (var i = 3; i < documents.Count; i++)
|
||||||
|
{
|
||||||
|
documents[i].GroupingKey = 5;
|
||||||
|
documents[i].SomeContent = $"{content}-{i}";
|
||||||
|
}
|
||||||
|
var guid1 = Guid.NewGuid().ToString("n");
|
||||||
|
var guid2 = Guid.NewGuid().ToString("n");
|
||||||
|
for (var i = 0; i < documents.Count - 1; i++)
|
||||||
|
{
|
||||||
|
documents[i].Children = new List<Child> {
|
||||||
|
new Child(guid1, guid2)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = SUT.GroupBy<T, int, ProjectedGroup, TKey>(
|
||||||
|
e => e.Children.Any(c => c.Type == guid1),
|
||||||
|
e => e.GroupingKey,
|
||||||
|
g => new ProjectedGroup
|
||||||
|
{
|
||||||
|
Key = g.Key,
|
||||||
|
Content = g.Select(doc => doc.SomeContent).ToList()
|
||||||
|
},
|
||||||
|
PartitionKey);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var key1Group = result.First(e => e.Key == 4);
|
||||||
|
Assert.NotNull(key1Group);
|
||||||
|
Assert.Equal(3, key1Group.Content.Count);
|
||||||
|
var key2Group = result.First(e => e.Key == 5);
|
||||||
|
Assert.NotNull(key2Group);
|
||||||
|
Assert.Single(key2Group.Content);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Group By
|
||||||
|
|
||||||
|
#region Pagination
|
||||||
|
|
||||||
|
public static Random Random = new Random();
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetSortedPaginatedAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var content = $"{Guid.NewGuid()}";
|
||||||
|
var documents = CreateTestDocuments(10);
|
||||||
|
for (var i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
documents[i].GroupingKey = 8;
|
||||||
|
documents[i].Nested.SomeAmount = Random.Next(1, 500000);
|
||||||
|
documents[i].SomeContent = content;
|
||||||
|
}
|
||||||
|
for (var i = 5; i < documents.Count; i++)
|
||||||
|
{
|
||||||
|
documents[i].GroupingKey = 9;
|
||||||
|
documents[i].SomeContent = content;
|
||||||
|
}
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
|
||||||
|
documents = documents.OrderByDescending(e => e.Nested.SomeAmount).ToList();
|
||||||
|
var notExpected = documents.First();
|
||||||
|
var expectedFirstResult = documents[1];
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await SUT.GetSortedPaginatedAsync<T, TKey>(
|
||||||
|
e => e.GroupingKey == 8 && e.SomeContent == content,
|
||||||
|
e => e.Nested.SomeAmount,
|
||||||
|
false,
|
||||||
|
1,5,
|
||||||
|
PartitionKey);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(4, result.Count);
|
||||||
|
Assert.True(!result.Contains(notExpected));
|
||||||
|
Assert.Equal(expectedFirstResult.Id, result[0].Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetSortedPaginatedAsyncWithSortOptions()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var content = $"{Guid.NewGuid()}";
|
||||||
|
var documents = CreateTestDocuments(10);
|
||||||
|
for (var i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
documents[i].GroupingKey = 8;
|
||||||
|
documents[i].Nested.SomeAmount = Random.Next(1, 500000);
|
||||||
|
documents[i].SomeContent = content;
|
||||||
|
}
|
||||||
|
for (var i = 5; i < documents.Count; i++)
|
||||||
|
{
|
||||||
|
documents[i].GroupingKey = 9;
|
||||||
|
documents[i].SomeContent = content;
|
||||||
|
}
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
|
||||||
|
documents = documents.OrderByDescending(e => e.Nested.SomeAmount).ToList();
|
||||||
|
var notExpected = documents.First();
|
||||||
|
var expectedFirstResult = documents[1];
|
||||||
|
var sorting = Builders<T>.Sort.Descending(e => e.Nested.SomeAmount);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await SUT.GetSortedPaginatedAsync<T, TKey>(
|
||||||
|
e => e.GroupingKey == 8 && e.SomeContent == content,
|
||||||
|
sorting,
|
||||||
|
1, 5,
|
||||||
|
PartitionKey);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(4, result.Count);
|
||||||
|
Assert.True(!result.Contains(notExpected));
|
||||||
|
Assert.Equal(expectedFirstResult.Id, result[0].Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Pagination
|
||||||
|
|
||||||
#region Test Utils
|
#region Test Utils
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||||
private string GetCurrentMethod()
|
private string GetCurrentMethod()
|
||||||
{
|
{
|
||||||
@@ -0,0 +1,377 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CoreIntegrationTests.Infrastructure
|
||||||
|
{
|
||||||
|
public abstract partial class MongoDbTKeyDocumentTestBase<T, TKey> :
|
||||||
|
IClassFixture<MongoDbTestFixture<T, TKey>>
|
||||||
|
where T : TestDoc<TKey>, new()
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
|
||||||
|
{
|
||||||
|
#region Update One
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateOne()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T, TKey>(document);
|
||||||
|
var content = GetContent();
|
||||||
|
document.SomeContent = content;
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateOne<T, TKey>(document);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result);
|
||||||
|
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument, GetTestName());
|
||||||
|
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateOneAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T, TKey>(document);
|
||||||
|
var content = GetContent();
|
||||||
|
document.SomeContent = content;
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateOneAsync<T, TKey>(document);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result);
|
||||||
|
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument, GetTestName());
|
||||||
|
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateOneField()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T, TKey>(document);
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateOne<T, TKey, string>(document, x => x.SomeContent, content);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument, GetTestName());
|
||||||
|
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateOneFieldAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T, TKey>(document);
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateOneAsync<T, TKey, string>(document, x => x.SomeContent, content);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument, GetTestName());
|
||||||
|
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateOneFieldWithFilter()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T, TKey>(document);
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateOne<T, TKey, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument, GetTestName());
|
||||||
|
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateOneFieldWithFilterAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T, TKey>(document);
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateOneAsync<T, TKey, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument, GetTestName());
|
||||||
|
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateOneAsyncWithUpdateDefinition()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T, TKey>(document);
|
||||||
|
var childrenToAdd = new List<Child>
|
||||||
|
{
|
||||||
|
new Child("testType1", "testValue1"),
|
||||||
|
new Child("testType2", "testValue2")
|
||||||
|
};
|
||||||
|
|
||||||
|
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateOneAsync<T, TKey>(document, updateDef);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result);
|
||||||
|
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument);
|
||||||
|
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateOneWithUpdateDefinition()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
SUT.AddOne<T, TKey>(document);
|
||||||
|
var childrenToAdd = new List<Child>
|
||||||
|
{
|
||||||
|
new Child("testType1", "testValue1"),
|
||||||
|
new Child("testType2", "testValue2")
|
||||||
|
};
|
||||||
|
|
||||||
|
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateOne<T, TKey>(document, updateDef);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result);
|
||||||
|
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||||
|
Assert.True(null != updatedDocument);
|
||||||
|
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Update One
|
||||||
|
|
||||||
|
#region Update Many
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateManyWithLinqFilterAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateManyAsync<T, TKey, string>(x => docIds.Contains(x.Id), x => x.SomeContent, content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetAll<T, TKey>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocument.Count == 2);
|
||||||
|
Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateManyWithFilterDefinitionAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var filterDefinition = Builders<T>.Filter.Where(x => docIds.Contains(x.Id));
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateManyAsync<T, TKey, string>(filterDefinition, x => x.SomeContent, content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetAll<T, TKey>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocument.Count == 2);
|
||||||
|
Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateManyWithLinqFilterAndUpdateDefinitionAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var childrenToAdd = new List<Child>
|
||||||
|
{
|
||||||
|
new Child("testType1", "testValue1"),
|
||||||
|
new Child("testType2", "testValue2")
|
||||||
|
};
|
||||||
|
|
||||||
|
var updateDef = Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateManyAsync<T, TKey>(x => docIds.Contains(x.Id), updateDef, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocuments = SUT.GetAll<T, TKey>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocuments.Count == 2);
|
||||||
|
updatedDocuments.ForEach(updatedDocument =>
|
||||||
|
{
|
||||||
|
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateManyWithFilterAndUpdateDefinitionsAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
var childrenToAdd = new List<Child>
|
||||||
|
{
|
||||||
|
new Child("testType1", "testValue1"),
|
||||||
|
new Child("testType2", "testValue2")
|
||||||
|
};
|
||||||
|
|
||||||
|
var updateDef = Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||||
|
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var filterDefinition = Builders<T>.Filter.Where(x => docIds.Contains(x.Id));
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = await SUT.UpdateManyAsync<T, TKey>(filterDefinition, updateDef, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocuments = SUT.GetAll<T, TKey>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocuments.Count == 2);
|
||||||
|
updatedDocuments.ForEach(updatedDocument =>
|
||||||
|
{
|
||||||
|
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateManyWithLinqFilter()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateMany<T, TKey, string>(x => docIds.Contains(x.Id), x => x.SomeContent, content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetAll<T, TKey>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
Assert.True(updatedDocument.Count == 2);
|
||||||
|
Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateManyWithFilterDefinition()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var filterDefinition = Builders<T>.Filter.Where(x => docIds.Contains(x.Id));
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateMany<T, TKey, string>(filterDefinition, x => x.SomeContent, content, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocument = SUT.GetAll<T, TKey>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocument.Count == 2);
|
||||||
|
Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateManyWithLinqFilterAndUpdateDefinition()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var childrenToAdd = new List<Child>
|
||||||
|
{
|
||||||
|
new Child("testType1", "testValue1"),
|
||||||
|
new Child("testType2", "testValue2")
|
||||||
|
};
|
||||||
|
|
||||||
|
var updateDef = Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateMany<T, TKey>(x => docIds.Contains(x.Id), updateDef, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocuments = SUT.GetAll<T, TKey>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocuments.Count == 2);
|
||||||
|
updatedDocuments.ForEach(updatedDocument =>
|
||||||
|
{
|
||||||
|
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateManyWithFilterAndUpdateDefinitions()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var documents = CreateTestDocuments(2);
|
||||||
|
SUT.AddMany<T, TKey>(documents);
|
||||||
|
var childrenToAdd = new List<Child>
|
||||||
|
{
|
||||||
|
new Child("testType1", "testValue1"),
|
||||||
|
new Child("testType2", "testValue2")
|
||||||
|
};
|
||||||
|
|
||||||
|
var updateDef = Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||||
|
|
||||||
|
var docIds = documents.Select(u => u.Id).ToArray();
|
||||||
|
var filterDefinition = Builders<T>.Filter.Where(x => docIds.Contains(x.Id));
|
||||||
|
var content = GetContent();
|
||||||
|
// Act
|
||||||
|
var result = SUT.UpdateMany<T, TKey>(filterDefinition, updateDef, PartitionKey);
|
||||||
|
// Assert
|
||||||
|
Assert.True(result == 2, GetTestName());
|
||||||
|
var updatedDocuments = SUT.GetAll<T, TKey>(x => docIds.Contains(x.Id), PartitionKey);
|
||||||
|
|
||||||
|
Assert.True(updatedDocuments.Count == 2);
|
||||||
|
updatedDocuments.ForEach(updatedDocument =>
|
||||||
|
{
|
||||||
|
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||||
|
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion Update Many
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,32 +5,29 @@ using System.Collections.Concurrent;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
|
|
||||||
namespace CoreIntegrationTests.Infrastructure
|
namespace CoreIntegrationTests.Infrastructure
|
||||||
{
|
{
|
||||||
|
|
||||||
public class MongoDbTestFixture<T, TKey> : IDisposable
|
public class MongoDbTestFixture<T, TKey> : IDisposable
|
||||||
where T : IDocument<TKey>, new()
|
where T : IDocument<TKey>, new()
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
|
|
||||||
public IMongoDbContext Context;
|
public IMongoDbContext Context;
|
||||||
|
|
||||||
public MongoDbTestFixture()
|
public MongoDbTestFixture()
|
||||||
{
|
{
|
||||||
DocsToDelete = new ConcurrentBag<T>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string PartitionKey { get; set; }
|
public string PartitionKey { get; set; }
|
||||||
|
|
||||||
public ConcurrentBag<T> DocsToDelete { get; set; }
|
public static ConcurrentBag<T> DocsToDelete { get; set; } = new ConcurrentBag<T>();
|
||||||
|
|
||||||
public virtual void Dispose()
|
public virtual void Dispose()
|
||||||
{
|
{
|
||||||
var docIds = DocsToDelete.ToList().Select(e => e.Id);
|
|
||||||
if (docIds.Any())
|
if (DocsToDelete.Any())
|
||||||
{
|
{
|
||||||
TestRepository.Instance.DeleteMany<T, TKey>(e => docIds.Contains(e.Id));
|
TestRepository.Instance.DeleteMany<T, TKey>(DocsToDelete.ToList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,8 +43,9 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
var docs = new List<T>();
|
var docs = new List<T>();
|
||||||
for (var i = 0; i < numberOfDocumentsToCreate; i++)
|
for (var i = 0; i < numberOfDocumentsToCreate; i++)
|
||||||
{
|
{
|
||||||
docs.Add(new T());
|
var doc = new T();
|
||||||
DocsToDelete.Add(docs.Last());
|
docs.Add(doc);
|
||||||
|
DocsToDelete.Add(doc);
|
||||||
}
|
}
|
||||||
return docs;
|
return docs;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using MongoDB.Bson.Serialization.Attributes;
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
using MongoDbGenericRepository.Models;
|
using MongoDbGenericRepository.Models;
|
||||||
using MongoDbGenericRepository.Utils;
|
using MongoDbGenericRepository.Utils;
|
||||||
using System;
|
using System;
|
||||||
@@ -21,6 +22,8 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
public class Nested
|
public class Nested
|
||||||
{
|
{
|
||||||
public DateTime SomeDate { get; set; }
|
public DateTime SomeDate { get; set; }
|
||||||
|
[BsonRepresentation(BsonType.Decimal128)]
|
||||||
|
public decimal SomeAmount { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Child
|
public class Child
|
||||||
@@ -79,6 +82,7 @@ namespace CoreIntegrationTests.Infrastructure
|
|||||||
Children = new List<Child>();
|
Children = new List<Child>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int GroupingKey { get; set; }
|
||||||
public string SomeContent { get; set; }
|
public string SomeContent { get; set; }
|
||||||
public string SomeContent4 { get; set; }
|
public string SomeContent4 { get; set; }
|
||||||
public string SomeContent5 { get; set; }
|
public string SomeContent5 { get; set; }
|
||||||
|
|||||||
@@ -1,7 +1,43 @@
|
|||||||
using MongoDbGenericRepository;
|
using MongoDB.Bson;
|
||||||
|
using MongoDbGenericRepository;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace CoreIntegrationTests.Infrastructure
|
namespace CoreIntegrationTests.Infrastructure
|
||||||
{
|
{
|
||||||
|
public interface ITestRepository<TKey> : IBaseMongoRepository<TKey> where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
void DropTestCollection<TDocument>();
|
||||||
|
void DropTestCollection<TDocument>(string partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TestTKeyRepository<TKey> : BaseMongoRepository<TKey>, ITestRepository<TKey> where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
const string connectionString = "mongodb://localhost:27017/MongoDbTests";
|
||||||
|
private static readonly ITestRepository<TKey> _instance = new TestTKeyRepository<TKey>(connectionString);
|
||||||
|
/// <inheritdoc />
|
||||||
|
private TestTKeyRepository(string connectionString) : base(connectionString)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ITestRepository<TKey> Instance
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DropTestCollection<TDocument>()
|
||||||
|
{
|
||||||
|
MongoDbContext.DropCollection<TDocument>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DropTestCollection<TDocument>(string partitionKey)
|
||||||
|
{
|
||||||
|
MongoDbContext.DropCollection<TDocument>(partitionKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A singleton implementation of the TestRepository
|
/// A singleton implementation of the TestRepository
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -11,10 +11,6 @@
|
|||||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||||
<dependentAssembly>
|
<dependentAssembly>
|
||||||
<assemblyIdentity name="System.Runtime.InteropServices.RuntimeInformation" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
<assemblyIdentity name="System.Runtime.InteropServices.RuntimeInformation" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
||||||
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
|
|
||||||
</dependentAssembly>
|
|
||||||
<dependentAssembly>
|
|
||||||
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
|
|
||||||
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
|
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
|
||||||
</dependentAssembly>
|
</dependentAssembly>
|
||||||
</assemblyBinding>
|
</assemblyBinding>
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ namespace IntegrationTests.Infrastructure
|
|||||||
SUT.AddOne<T>(document);
|
SUT.AddOne<T>(document);
|
||||||
// Act
|
// Act
|
||||||
var cursor = SUT.GetCursor<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
var cursor = SUT.GetCursor<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||||
var count = cursor.Count();
|
var count = cursor.CountDocuments();
|
||||||
// Assert
|
// Assert
|
||||||
Assert.AreEqual(1, count, GetTestName());
|
Assert.AreEqual(1, count, GetTestName());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -204,7 +204,7 @@ namespace IntegrationTests.Infrastructure
|
|||||||
SUT.AddOne<T, TKey>(document);
|
SUT.AddOne<T, TKey>(document);
|
||||||
// Act
|
// Act
|
||||||
var cursor = SUT.GetCursor<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
var cursor = SUT.GetCursor<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||||
var count = cursor.Count();
|
var count = cursor.CountDocuments();
|
||||||
// Assert
|
// Assert
|
||||||
Assert.AreEqual(1, count, GetTestName());
|
Assert.AreEqual(1, count, GetTestName());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<Import Project="..\packages\NUnit3TestAdapter.3.15.1\build\net35\NUnit3TestAdapter.props" Condition="Exists('..\packages\NUnit3TestAdapter.3.15.1\build\net35\NUnit3TestAdapter.props')" />
|
||||||
|
<Import Project="..\packages\NUnit.3.12.0\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.12.0\build\NUnit.props')" />
|
||||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
@@ -11,6 +13,8 @@
|
|||||||
<AssemblyName>IntegrationTests</AssemblyName>
|
<AssemblyName>IntegrationTests</AssemblyName>
|
||||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||||
<FileAlignment>512</FileAlignment>
|
<FileAlignment>512</FileAlignment>
|
||||||
|
<NuGetPackageImportStamp>
|
||||||
|
</NuGetPackageImportStamp>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
@@ -30,33 +34,44 @@
|
|||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="DnsClient, Version=1.0.7.0, Culture=neutral, PublicKeyToken=4574bb5573c51424, processorArchitecture=MSIL">
|
<Reference Include="Crc32C.NET, Version=1.0.5.0, Culture=neutral, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.8\lib\net45\DnsClient.dll</HintPath>
|
<HintPath>..\packages\Crc32C.NET.1.0.5.0\lib\net20\Crc32C.NET.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="MongoDB.Bson, Version=2.7.0.0, Culture=neutral, processorArchitecture=MSIL">
|
<Reference Include="DnsClient, Version=1.2.0.0, Culture=neutral, PublicKeyToken=4574bb5573c51424, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.8\lib\net45\MongoDB.Bson.dll</HintPath>
|
<HintPath>..\packages\DnsClient.1.2.0\lib\net45\DnsClient.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="MongoDB.Driver, Version=2.7.0.0, Culture=neutral, processorArchitecture=MSIL">
|
<Reference Include="MongoDB.Bson, Version=2.9.3.0, Culture=neutral, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.8\lib\net45\MongoDB.Driver.dll</HintPath>
|
<HintPath>..\packages\MongoDB.Bson.2.9.3\lib\net452\MongoDB.Bson.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="MongoDB.Driver.Core, Version=2.7.0.0, Culture=neutral, processorArchitecture=MSIL">
|
<Reference Include="MongoDB.Driver, Version=2.9.3.0, Culture=neutral, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.8\lib\net45\MongoDB.Driver.Core.dll</HintPath>
|
<HintPath>..\packages\MongoDB.Driver.2.9.3\lib\net452\MongoDB.Driver.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="MongoDbGenericRepository, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
<Reference Include="MongoDB.Driver.Core, Version=2.9.3.0, Culture=neutral, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.8\lib\net45\MongoDbGenericRepository.dll</HintPath>
|
<HintPath>..\packages\MongoDB.Driver.Core.2.9.3\lib\net452\MongoDB.Driver.Core.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="nunit.framework, Version=3.9.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
|
<Reference Include="MongoDbGenericRepository, Version=1.4.4.0, Culture=neutral, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\NUnit.3.9.0\lib\net45\nunit.framework.dll</HintPath>
|
<HintPath>..\packages\MongoDbGenericRepository.1.4.4\lib\net452\MongoDbGenericRepository.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="nunit.framework, Version=3.12.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\NUnit.3.12.0\lib\net45\nunit.framework.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="SharpCompress, Version=0.23.0.0, Culture=neutral, PublicKeyToken=afb0a02973931d96, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\SharpCompress.0.23.0\lib\net45\SharpCompress.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="Snappy.NET, Version=1.1.1.8, Culture=neutral, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\Snappy.NET.1.1.1.8\lib\net45\Snappy.NET.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Buffers, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.8\lib\net45\System.Buffers.dll</HintPath>
|
<HintPath>..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System.Configuration" />
|
<Reference Include="System.Configuration" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.8\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
|
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
|
||||||
|
<Private>True</Private>
|
||||||
|
<Private>True</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -87,4 +102,10 @@
|
|||||||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||||
|
<PropertyGroup>
|
||||||
|
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Error Condition="!Exists('..\packages\NUnit3TestAdapter.3.15.1\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit3TestAdapter.3.15.1\build\net35\NUnit3TestAdapter.props'))" />
|
||||||
|
</Target>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,12 +1,16 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<packages>
|
<packages>
|
||||||
<package id="DnsClient" version="1.0.7" targetFramework="net461" />
|
<package id="Crc32C.NET" version="1.0.5.0" targetFramework="net461" />
|
||||||
<package id="MongoDB.Bson" version="2.7.0" targetFramework="net461" />
|
<package id="DnsClient" version="1.2.0" targetFramework="net461" />
|
||||||
<package id="MongoDB.Driver" version="2.7.0" targetFramework="net461" />
|
<package id="MongoDB.Bson" version="2.9.3" targetFramework="net461" />
|
||||||
<package id="MongoDB.Driver.Core" version="2.7.0" targetFramework="net461" />
|
<package id="MongoDB.Driver" version="2.9.3" targetFramework="net461" />
|
||||||
<package id="MongoDbGenericRepository" version="1.3.8" targetFramework="net461" />
|
<package id="MongoDB.Driver.Core" version="2.9.3" targetFramework="net461" />
|
||||||
<package id="NUnit" version="3.9.0" targetFramework="net461" />
|
<package id="MongoDbGenericRepository" version="1.4.4" targetFramework="net461" />
|
||||||
<package id="NUnit.ConsoleRunner" version="3.7.0" targetFramework="net461" />
|
<package id="NUnit" version="3.12.0" targetFramework="net461" />
|
||||||
<package id="System.Buffers" version="4.3.0" targetFramework="net461" />
|
<package id="NUnit.ConsoleRunner" version="3.10.0" targetFramework="net461" />
|
||||||
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.0.0" targetFramework="net461" />
|
<package id="NUnit3TestAdapter" version="3.15.1" targetFramework="net461" />
|
||||||
|
<package id="SharpCompress" version="0.23.0" targetFramework="net461" />
|
||||||
|
<package id="Snappy.NET" version="1.1.1.8" targetFramework="net461" />
|
||||||
|
<package id="System.Buffers" version="4.4.0" targetFramework="net461" />
|
||||||
|
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" />
|
||||||
</packages>
|
</packages>
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
<SolutionConfiguration>
|
|
||||||
<Settings>
|
|
||||||
<CurrentEngineMode>Run all tests automatically [Global]</CurrentEngineMode>
|
|
||||||
</Settings>
|
|
||||||
</SolutionConfiguration>
|
|
||||||
@@ -1,612 +1,22 @@
|
|||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using MongoDbGenericRepository.Models;
|
using System.Threading.Tasks;
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace MongoDbGenericRepository
|
namespace MongoDbGenericRepository
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The IBaseMongoRepository exposes the CRUD functionality of the BaseMongoRepository.
|
/// The IBaseMongoRepository interface exposes the CRUD functionality of the BaseMongoRepository.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IBaseMongoRepository : IReadOnlyMongoRepository, IMongoDbCollectionIndexRepository
|
public interface IBaseMongoRepository :
|
||||||
|
IReadOnlyMongoRepository,
|
||||||
|
IBaseMongoRepository_Create,
|
||||||
|
IBaseMongoRepository_Update,
|
||||||
|
IBaseMongoRepository_Delete,
|
||||||
|
IBaseMongoRepository_Index
|
||||||
{
|
{
|
||||||
#region Create
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously adds a document to the collection.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="document">The document you want to add.</param>
|
|
||||||
Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a document to the collection.
|
|
||||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="document">The document you want to add.</param>
|
|
||||||
void AddOne<TDocument>(TDocument document) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously adds a list of documents to the collection.
|
|
||||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="documents">The document you want to add.</param>
|
|
||||||
Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a list of documents to the collection.
|
|
||||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="documents">The document you want to add.</param>
|
|
||||||
void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Create TKey
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously adds a document to the collection.
|
|
||||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
|
||||||
/// </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="document">The document you want to add.</param>
|
|
||||||
Task AddOneAsync<TDocument, TKey>(TDocument document)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a document to the collection.
|
|
||||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
|
||||||
/// </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="document">The document you want to add.</param>
|
|
||||||
void AddOne<TDocument, TKey>(TDocument document)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously adds a list of documents to the collection.
|
|
||||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
|
||||||
/// </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="documents">The documents you want to add.</param>
|
|
||||||
Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a list of documents to the collection.
|
|
||||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
|
||||||
/// </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="documents">The documents you want to add.</param>
|
|
||||||
void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Update
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously Updates a document.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
|
||||||
Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Updates a document.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
|
||||||
bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <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="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>
|
|
||||||
bool UpdateOne<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <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="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>
|
|
||||||
Task<bool> UpdateOneAsync<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <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="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>
|
|
||||||
bool UpdateOne<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <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="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>
|
|
||||||
Task<bool> UpdateOneAsync<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <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="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>
|
|
||||||
bool UpdateOne<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <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>
|
|
||||||
/// <param name="documentToModify">The document you want to modify.</param>
|
|
||||||
/// <param name="update">The update definition for the document.</param>
|
|
||||||
Task<bool> UpdateOneAsync<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <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="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>
|
|
||||||
Task<bool> UpdateOneAsync<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <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>
|
|
||||||
/// <param name="documentToModify">The document you want to modify.</param>
|
|
||||||
/// <param name="update">The update definition for the document.</param>
|
|
||||||
bool UpdateOne<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Update TKey
|
|
||||||
|
|
||||||
/// <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>
|
|
||||||
Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument modifiedDocument)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <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>
|
|
||||||
bool UpdateOne<TDocument, TKey>(TDocument modifiedDocument)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <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>
|
|
||||||
Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <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>
|
|
||||||
bool UpdateOne<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <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>
|
|
||||||
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>;
|
|
||||||
|
|
||||||
/// <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>
|
|
||||||
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <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>
|
|
||||||
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>;
|
|
||||||
|
|
||||||
/// <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>
|
|
||||||
bool UpdateOne<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <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>
|
|
||||||
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>;
|
|
||||||
|
|
||||||
/// <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>
|
|
||||||
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>;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Delete
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously deletes a document.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="document">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">The type representing a Document.</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">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="document">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">The type representing a Document.</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">The type representing a Document.</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">The type representing a Document.</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">The type representing a Document.</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">The type representing a Document.</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
|
|
||||||
|
|
||||||
#region Delete TKey
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deletes 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="document">The document you want to delete.</param>
|
|
||||||
/// <returns>The number of documents deleted.</returns>
|
|
||||||
long DeleteOne<TDocument, TKey>(TDocument document)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
|
||||||
/// </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="document">The document you want to delete.</param>
|
|
||||||
/// <returns>The number of documents deleted.</returns>
|
|
||||||
Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deletes a document matching the condition of the LINQ expression filter.
|
|
||||||
/// </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="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
/// <returns>The number of documents deleted.</returns>
|
|
||||||
long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
|
||||||
/// </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="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, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
|
|
||||||
/// </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="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, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously deletes a list of documents.
|
|
||||||
/// </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="documents">The list of documents to delete.</param>
|
|
||||||
/// <returns>The number of documents deleted.</returns>
|
|
||||||
Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deletes a list of documents.
|
|
||||||
/// </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="documents">The list of documents to delete.</param>
|
|
||||||
/// <returns>The number of documents deleted.</returns>
|
|
||||||
long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deletes the documents matching the condition of the LINQ expression filter.
|
|
||||||
/// </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="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
/// <returns>The number of documents deleted.</returns>
|
|
||||||
long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Project
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns a projected document matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <param name="projection">The projection expression.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<TProjection> ProjectOneAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
where TProjection : class;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns a projected document matching the filter condition.
|
|
||||||
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <param name="projection">The projection expression.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<TProjection> ProjectOneAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
where TProjection : class;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a projected document matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <param name="projection">The projection expression.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
TProjection ProjectOne<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
where TProjection : class;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a projected document matching the filter condition.
|
|
||||||
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <param name="projection">The projection expression.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
TProjection ProjectOne<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
where TProjection : class;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns a list of projected documents matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <param name="projection">The projection expression.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
where TProjection : class;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns a list of projected documents matching the filter condition.
|
|
||||||
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <param name="projection">The projection expression.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
where TProjection : class;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns a list of projected documents matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <param name="projection">The projection expression.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
List<TProjection> ProjectMany<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
where TProjection : class;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns a list of projected documents matching the filter condition.
|
|
||||||
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <param name="projection">The projection expression.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
List<TProjection> ProjectMany<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
where TProjection : class;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -654,45 +64,6 @@ namespace MongoDbGenericRepository
|
|||||||
Task<TDocument> GetAndUpdateOne<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options)
|
Task<TDocument> GetAndUpdateOne<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>;
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
#region Grouping
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Groups a collection of documents given a grouping criteria,
|
|
||||||
/// and returns a list of projected documents.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam>
|
|
||||||
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
|
||||||
/// <param name="groupingCriteria">The grouping criteria.</param>
|
|
||||||
/// <param name="groupProjection">The projected group result.</param>
|
|
||||||
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
|
||||||
List<TProjection> GroupBy<TDocument, TGroupKey, TProjection>(
|
|
||||||
Expression<Func<TDocument, TGroupKey>> groupingCriteria,
|
|
||||||
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection,
|
|
||||||
string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
where TProjection : class, new();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Groups filtered a collection of documents given a grouping criteria,
|
|
||||||
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam>
|
|
||||||
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
|
||||||
/// <param name="filter"></param>
|
|
||||||
/// <param name="selector">The grouping criteria.</param>
|
|
||||||
/// <param name="projection">The projected group result.</param>
|
|
||||||
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
|
||||||
List<TProjection> GroupBy<TDocument, TGroupKey, TProjection>(Expression<Func<TDocument, bool>> filter,
|
|
||||||
Expression<Func<TDocument, TGroupKey>> selector,
|
|
||||||
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> projection,
|
|
||||||
string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
where TProjection : class, new();
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,239 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
public interface IBaseMongoRepository_Update : IBaseMongoRepository_Update<Guid>
|
||||||
|
{
|
||||||
|
/// <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>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument modifiedDocument)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
bool UpdateOne<TDocument, TKey>(TDocument modifiedDocument)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
bool UpdateOne<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
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>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
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>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
bool UpdateOne<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
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>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
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>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities 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>
|
||||||
|
Task<long> UpdateManyAsync<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>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities 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 value of the partition key.</param>
|
||||||
|
Task<long> UpdateManyAsync<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, 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="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
Task<long> UpdateManyAsync<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, 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="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
Task<long> UpdateManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities 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>
|
||||||
|
long UpdateMany<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>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities 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 value of the partition key.</param>
|
||||||
|
long UpdateMany<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, 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="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
long UpdateMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, 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="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
long UpdateMany<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,166 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository.DataAccess.Update
|
||||||
|
{
|
||||||
|
public interface IBaseMongoRepository_Update_ClientSession
|
||||||
|
{
|
||||||
|
/// <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="session">The client session.</param>
|
||||||
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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="session">The client session.</param>
|
||||||
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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="session">The client session.</param>
|
||||||
|
/// <param name="documentToModify">The document to modify.</param>
|
||||||
|
/// <param name="update">The update definition.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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="session">The client session.</param>
|
||||||
|
/// <param name="documentToModify">The document to modify.</param>
|
||||||
|
/// <param name="update">The update definition.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="documentToModify">The document to modify.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="documentToModify">The document to modify.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="filter">The filter for the update.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="partitionKey">The optional partition key.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="filter">The filter for the update.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="partitionKey">The optional partition key.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="filter">The filter for the update.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="partitionKey">The optional partition key.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="filter">The filter for the update.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="partitionKey">The optional partition key.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,464 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The IBaseReadOnlyRepository exposes the generic Read Only functionality of the BaseMongoRepository.
|
||||||
|
/// </summary>
|
||||||
|
public interface IBaseReadOnlyRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The connection string.
|
||||||
|
/// </summary>
|
||||||
|
string ConnectionString { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The database name.
|
||||||
|
/// </summary>
|
||||||
|
string DatabaseName { get; }
|
||||||
|
|
||||||
|
#region Read TKey
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns one document given its id.
|
||||||
|
/// </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="id">The Id of the document you want to get.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<TDocument> GetByIdAsync<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns one document given its id.
|
||||||
|
/// </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="id">The Id of the document you want to get.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
TDocument GetById<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns one document given an expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns one document given an expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a collection cursor.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if any of the document of the collection matches the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if any of the document of the collection matches the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously counts how many documents match the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey</param>
|
||||||
|
Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Counts how many documents match the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey</param>
|
||||||
|
long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Min / Max
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByDescending">A property selector to order by descending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByDescending">A property selector to order by descending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
TDocument GetByMax<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
Task<TDocument> GetByMinAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
TDocument GetByMin<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
Task<TValue> GetMaxValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> orderByAscending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByDescending">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
TValue GetMaxValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> orderByDescending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<TValue> GetMinValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
TValue GetMinValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Sum
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
Task<int> SumByAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, int>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
int SumBy<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, int>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
Task<decimal> SumByAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, decimal>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
decimal SumBy<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, decimal>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
#endregion Sum
|
||||||
|
|
||||||
|
#region Project TKey
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a projected document matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<TProjection> ProjectOneAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a projected document matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
TProjection ProjectOne<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
List<TProjection> ProjectMany<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Group By
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Groups filtered a collection of documents given a grouping criteria,
|
||||||
|
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="groupingCriteria">The grouping criteria.</param>
|
||||||
|
/// <param name="groupProjection">The projected group result.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
List<TProjection> GroupBy<TDocument, TGroupKey, TProjection, TKey>(
|
||||||
|
Expression<Func<TDocument, TGroupKey>> groupingCriteria,
|
||||||
|
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class, new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Groups filtered a collection of documents given a grouping criteria,
|
||||||
|
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="groupingCriteria">The grouping criteria.</param>
|
||||||
|
/// <param name="groupProjection">The projected group result.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
List<TProjection> GroupBy<TDocument, TGroupKey, TProjection, TKey>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, TGroupKey>> groupingCriteria,
|
||||||
|
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class, new();
|
||||||
|
|
||||||
|
#endregion Group By
|
||||||
|
|
||||||
|
#region Pagination
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="sortSelector">The property selector.</param>
|
||||||
|
/// <param name="ascending">Order of the sorting.</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>
|
||||||
|
Task<List<TDocument>> GetSortedPaginatedAsync<TDocument, TKey>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, object>> sortSelector,
|
||||||
|
bool ascending = true,
|
||||||
|
int skipNumber = 0,
|
||||||
|
int takeNumber = 50,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="sortDefinition">The sort definition.</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>
|
||||||
|
Task<List<TDocument>> GetSortedPaginatedAsync<TDocument, TKey>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
SortDefinition<TDocument> sortDefinition,
|
||||||
|
int skipNumber = 0,
|
||||||
|
int takeNumber = 50,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
#endregion Pagination
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,381 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
public interface IReadOnlyMongoRepository<TKey> where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
#region Read
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns one document given its id.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="id">The Id of the document you want to get.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<TDocument> GetByIdAsync<TDocument>(TKey id, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns one document given its id.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="id">The Id of the document you want to get.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
TDocument GetById<TDocument>(TKey id, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns one document given an expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns one document given an expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a collection cursor.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns true if any of the document of the collection matches the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if any of the document of the collection matches the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of the documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a list of the documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously counts how many documents match the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Counts how many documents match the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Min / Max
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByDescending">A property selector to order by descending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
Task<TDocument> GetByMaxAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByDescending">A property selector to order by descending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
TDocument GetByMax<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
Task<TDocument> GetByMinAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
TDocument GetByMin<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
Task<TValue> GetMaxValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
TValue GetMaxValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<TValue> GetMinValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
TValue GetMinValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Maths
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
Task<int> SumByAsync<TDocument>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, int>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
Task<decimal> SumByAsync<TDocument>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, decimal>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
int SumBy<TDocument>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, int>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
decimal SumBy<TDocument>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, decimal>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
#endregion Maths
|
||||||
|
|
||||||
|
#region Project
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a projected document matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<TProjection> ProjectOneAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TProjection : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a projected document matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
TProjection ProjectOne<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TProjection : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TProjection : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
List<TProjection> ProjectMany<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TProjection : class;
|
||||||
|
|
||||||
|
#endregion Project
|
||||||
|
|
||||||
|
#region Group By
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Groups a collection of documents given a grouping criteria,
|
||||||
|
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
||||||
|
/// <param name="groupingCriteria">The grouping criteria.</param>
|
||||||
|
/// <param name="groupProjection">The projected group result.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
List<TProjection> GroupBy<TDocument, TGroupKey, TProjection>(
|
||||||
|
Expression<Func<TDocument, TGroupKey>> groupingCriteria,
|
||||||
|
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TProjection : class, new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Groups filtered a collection of documents given a grouping criteria,
|
||||||
|
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="groupingCriteria">The grouping criteria.</param>
|
||||||
|
/// <param name="groupProjection">The projected group result.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
List<TProjection> GroupBy<TDocument, TGroupKey, TProjection>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, TGroupKey>> groupingCriteria,
|
||||||
|
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TProjection : class, new();
|
||||||
|
|
||||||
|
#endregion Group By
|
||||||
|
|
||||||
|
#region Pagination
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="sortSelector">The property selector.</param>
|
||||||
|
/// <param name="ascending">Order of the sorting.</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>
|
||||||
|
Task<List<TDocument>> GetSortedPaginatedAsync<TDocument>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, object>> sortSelector,
|
||||||
|
bool ascending = true,
|
||||||
|
int skipNumber = 0,
|
||||||
|
int takeNumber = 50,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="sortDefinition">The sort definition.</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>
|
||||||
|
Task<List<TDocument>> GetSortedPaginatedAsync<TDocument>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
SortDefinition<TDocument> sortDefinition,
|
||||||
|
int skipNumber = 0,
|
||||||
|
int takeNumber = 50,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
#endregion Pagination
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,430 +1,12 @@
|
|||||||
using MongoDB.Driver;
|
using System;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using MongoDbGenericRepository.Models;
|
|
||||||
|
|
||||||
namespace MongoDbGenericRepository
|
namespace MongoDbGenericRepository
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The IReadOnlyMongoRepository exposes the readonly functionality of the BaseMongoRepository.
|
/// The IReadOnlyMongoRepository exposes the readonly functionality of the BaseMongoRepository.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IReadOnlyMongoRepository
|
public interface IReadOnlyMongoRepository : IBaseReadOnlyRepository, IReadOnlyMongoRepository<Guid>
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// The connection string.
|
|
||||||
/// </summary>
|
|
||||||
string ConnectionString { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// The database name.
|
|
||||||
/// </summary>
|
|
||||||
string DatabaseName { get; set; }
|
|
||||||
|
|
||||||
#region Read
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns one document given its id.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="id">The Id of the document you want to get.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<TDocument> GetByIdAsync<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns one document given its id.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="id">The Id of the document you want to get.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
TDocument GetById<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns one document given an expression filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns one document given an expression filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a collection cursor.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns true if any of the document of the collection matches the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if any of the document of the collection matches the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns a list of the documents matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a list of the documents matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously counts how many documents match the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Counts how many documents match the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Read TKey
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns one document given its id.
|
|
||||||
/// </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="id">The Id of the document you want to get.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<TDocument> GetByIdAsync<TDocument, TKey>(TKey id, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns one document given its id.
|
|
||||||
/// </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="id">The Id of the document you want to get.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
TDocument GetById<TDocument, TKey>(TKey id, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns one document given an expression filter.
|
|
||||||
/// </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="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns one document given an expression filter.
|
|
||||||
/// </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="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a collection cursor.
|
|
||||||
/// </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="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if any of the document of the collection matches the filter condition.
|
|
||||||
/// </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="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if any of the document of the collection matches the filter condition.
|
|
||||||
/// </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="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns a list of the documents matching the filter condition.
|
|
||||||
/// </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="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a list of the documents matching the filter condition.
|
|
||||||
/// </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="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously counts how many documents match the filter condition.
|
|
||||||
/// </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="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey</param>
|
|
||||||
Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Counts how many documents match the filter condition.
|
|
||||||
/// </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="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey</param>
|
|
||||||
long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Min / Max
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByDescending">A property selector to order by descending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
Task<TDocument> GetByMaxAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByDescending">A property selector to order by descending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
TDocument GetByMax<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
Task<TDocument> GetByMinAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
TDocument GetByMin<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByAscending">A property selector to order by descending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByAscending">A property selector to order by descending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
TDocument GetByMax<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
Task<TDocument> GetByMinAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
TDocument GetByMin<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
Task<TValue> GetMaxValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
Task<TValue> GetMaxValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
TValue GetMaxValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
TValue GetMaxValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> orderByDescending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<TValue> GetMinValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<TValue> GetMinValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
TValue GetMinValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
TValue GetMinValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,176 +0,0 @@
|
|||||||
using MongoDB.Driver;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using MongoDbGenericRepository.Models;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace MongoDbGenericRepository
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
|
|
||||||
/// Its constructor must be given a connection string and a database name.
|
|
||||||
/// </summary>
|
|
||||||
public abstract partial class BaseMongoRepository : ReadOnlyMongoRepository, IBaseMongoRepository
|
|
||||||
{
|
|
||||||
#region Index Management
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<string> CreateTextIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await CreateTextIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
{
|
|
||||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes
|
|
||||||
.CreateOneAsync(
|
|
||||||
new CreateIndexModel<TDocument>(
|
|
||||||
Builders<TDocument>.IndexKeys.Text(field),
|
|
||||||
indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions)
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<string> CreateAscendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await CreateAscendingIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
{
|
|
||||||
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
|
||||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
|
||||||
var indexKey = Builders<TDocument>.IndexKeys;
|
|
||||||
return await collection.Indexes
|
|
||||||
.CreateOneAsync(
|
|
||||||
new CreateIndexModel<TDocument>(indexKey.Ascending(field), createOptions));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<string> CreateDescendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await CreateDescendingIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
{
|
|
||||||
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
|
||||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
|
||||||
var indexKey = Builders<TDocument>.IndexKeys;
|
|
||||||
return await collection.Indexes
|
|
||||||
.CreateOneAsync(
|
|
||||||
new CreateIndexModel<TDocument>(indexKey.Descending(field), createOptions));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<string> CreateHashedIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await CreateHashedIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<string> CreateHashedIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
{
|
|
||||||
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
|
||||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
|
||||||
var indexKey = Builders<TDocument>.IndexKeys;
|
|
||||||
return await collection.Indexes
|
|
||||||
.CreateOneAsync(
|
|
||||||
new CreateIndexModel<TDocument>(indexKey.Hashed(field), createOptions));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<string> CreateCombinedTextIndexAsync<TDocument>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await CreateCombinedTextIndexAsync<TDocument, Guid>(fields, indexCreationOptions, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
{
|
|
||||||
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
|
||||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
|
||||||
var listOfDefs = new List<IndexKeysDefinition<TDocument>>();
|
|
||||||
foreach (var field in fields)
|
|
||||||
{
|
|
||||||
listOfDefs.Add(Builders<TDocument>.IndexKeys.Text(field));
|
|
||||||
}
|
|
||||||
return await collection.Indexes
|
|
||||||
.CreateOneAsync(new CreateIndexModel<TDocument>(Builders<TDocument>.IndexKeys.Combine(listOfDefs), createOptions));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task DropIndexAsync<TDocument>(string indexName, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
await DropIndexAsync<TDocument, Guid>(indexName, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
{
|
|
||||||
await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.DropOneAsync(indexName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<List<string>> GetIndexesNamesAsync<TDocument>(string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await GetIndexesNamesAsync<TDocument, Guid>(partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
{
|
|
||||||
var indexCursor = await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.ListAsync();
|
|
||||||
var indexes = await indexCursor.ToListAsync();
|
|
||||||
return indexes.Select(e => e["name"].ToString()).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endregion Index Management
|
|
||||||
|
|
||||||
private CreateIndexOptions MapIndexOptions(IndexCreationOptions indexCreationOptions)
|
|
||||||
{
|
|
||||||
return new CreateIndexOptions
|
|
||||||
{
|
|
||||||
Unique = indexCreationOptions.Unique,
|
|
||||||
TextIndexVersion = indexCreationOptions.TextIndexVersion,
|
|
||||||
SphereIndexVersion = indexCreationOptions.SphereIndexVersion,
|
|
||||||
Sparse = indexCreationOptions.Sparse,
|
|
||||||
Name = indexCreationOptions.Name,
|
|
||||||
Min = indexCreationOptions.Min,
|
|
||||||
Max = indexCreationOptions.Max,
|
|
||||||
LanguageOverride = indexCreationOptions.LanguageOverride,
|
|
||||||
ExpireAfter = indexCreationOptions.ExpireAfter,
|
|
||||||
DefaultLanguage = indexCreationOptions.DefaultLanguage,
|
|
||||||
BucketSize = indexCreationOptions.BucketSize,
|
|
||||||
Bits = indexCreationOptions.Bits,
|
|
||||||
Background = indexCreationOptions.Background,
|
|
||||||
Version = indexCreationOptions.Version
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,187 @@
|
|||||||
|
using MongoDbGenericRepository.DataAccess.Create;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
public interface IBaseMongoRepository_Create : IBaseMongoRepository_Create<Guid>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously adds a document to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </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="document">The document you want to add.</param>
|
||||||
|
Task AddOneAsync<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a document to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </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="document">The document you want to add.</param>
|
||||||
|
void AddOne<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously adds a list of documents to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </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="documents">The documents you want to add.</param>
|
||||||
|
Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a list of documents to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </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="documents">The documents you want to add.</param>
|
||||||
|
void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
|
||||||
|
/// Its constructor must be given a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create
|
||||||
|
{
|
||||||
|
private readonly object _initLock = new object();
|
||||||
|
private MongoDbCreator _mongoDbCreator;
|
||||||
|
protected virtual MongoDbCreator MongoDbCreator
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_mongoDbCreator != null) { return _mongoDbCreator; }
|
||||||
|
|
||||||
|
lock (_initLock)
|
||||||
|
{
|
||||||
|
if (_mongoDbCreator == null)
|
||||||
|
{
|
||||||
|
_mongoDbCreator = new MongoDbCreator(MongoDbContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return _mongoDbCreator;
|
||||||
|
}
|
||||||
|
set { _mongoDbCreator = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously adds a document to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </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="document">The document you want to add.</param>
|
||||||
|
public virtual async Task AddOneAsync<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
await MongoDbCreator.AddOneAsync<TDocument, TKey>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously adds a document to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="document">The document you want to add.</param>
|
||||||
|
public virtual async Task AddOneAsync<TDocument>(TDocument document)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
await MongoDbCreator.AddOneAsync<TDocument, Guid>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a document to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </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="document">The document you want to add.</param>
|
||||||
|
public virtual void AddOne<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
MongoDbCreator.AddOne<TDocument, TKey>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a document to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="document">The document you want to add.</param>
|
||||||
|
public virtual void AddOne<TDocument>(TDocument document) where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
MongoDbCreator.AddOne<TDocument, Guid>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously adds a list of documents to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </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="documents">The documents you want to add.</param>
|
||||||
|
public virtual async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
await MongoDbCreator.AddManyAsync<TDocument, TKey>(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously adds a list of documents to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="documents">The documents you want to add.</param>
|
||||||
|
public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
await MongoDbCreator.AddManyAsync<TDocument, Guid>(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a list of documents to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </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="documents">The documents you want to add.</param>
|
||||||
|
public virtual void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
MongoDbCreator.AddMany<TDocument, TKey>(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a list of documents to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="documents">The documents you want to add.</param>
|
||||||
|
public virtual void AddMany<TDocument>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
MongoDbCreator.AddMany<TDocument, Guid>(documents);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,344 @@
|
|||||||
|
using MongoDbGenericRepository.DataAccess.Delete;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
public interface IBaseMongoRepository_Delete : IBaseMongoRepository_Delete<Guid>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes 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="document">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
long DeleteOne<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="document">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="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, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="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, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a list of documents.
|
||||||
|
/// </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="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a list of documents.
|
||||||
|
/// </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="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract partial class BaseMongoRepository : IBaseMongoRepository_Delete
|
||||||
|
{
|
||||||
|
private MongoDbEraser _mongoDbEraser;
|
||||||
|
protected virtual MongoDbEraser MongoDbEraser
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_mongoDbEraser != null) { return _mongoDbEraser; }
|
||||||
|
|
||||||
|
lock (_initLock)
|
||||||
|
{
|
||||||
|
if (_mongoDbEraser == null)
|
||||||
|
{
|
||||||
|
_mongoDbEraser = new MongoDbEraser(MongoDbContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return _mongoDbEraser;
|
||||||
|
}
|
||||||
|
set { _mongoDbEraser = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Delete
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="document">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual async Task<long> DeleteOneAsync<TDocument>(TDocument document) where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbEraser.DeleteOneAsync<TDocument, Guid>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="document">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual long DeleteOne<TDocument>(TDocument document) where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbEraser.DeleteOne<TDocument, Guid>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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 virtual long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbEraser.DeleteOne<TDocument, Guid>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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 virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbEraser.DeleteOneAsync<TDocument, Guid>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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 virtual async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbEraser.DeleteManyAsync<TDocument, Guid>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a list of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await DeleteManyAsync<TDocument, Guid>(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a list of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return DeleteMany<TDocument, Guid>(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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 virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbEraser.DeleteMany<TDocument, Guid>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Delete
|
||||||
|
|
||||||
|
#region Delete TKey
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes 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="document">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual long DeleteOne<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbEraser.DeleteOne<TDocument, TKey>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="document">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbEraser.DeleteOneAsync<TDocument, TKey>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbEraser.DeleteOne<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbEraser.DeleteOneAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbEraser.DeleteManyAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a list of documents.
|
||||||
|
/// </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="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbEraser.DeleteManyAsync<TDocument, TKey>(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a list of documents.
|
||||||
|
/// </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="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbEraser.DeleteMany<TDocument, TKey>(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbEraser.DeleteMany<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,320 @@
|
|||||||
|
using MongoDbGenericRepository.DataAccess.Index;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
public interface IBaseMongoRepository_Index : IBaseMongoRepository_Index<Guid>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the names of the indexes present on a collection.
|
||||||
|
/// </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="partitionKey">An optional partition key</param>
|
||||||
|
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
|
||||||
|
Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a text index on the given field.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </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="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an index on the given field in ascending order.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </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="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an index on the given field in descending order.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </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="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a hashed index on the given field.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </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="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
Task<string> CreateHashedIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a combined text index.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </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="fields">The fields we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Drops the index given a field name
|
||||||
|
/// </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="indexName">The name of the index</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key</param>
|
||||||
|
Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
|
||||||
|
/// Its constructor must be given a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
public abstract partial class BaseMongoRepository : IBaseMongoRepository_Index
|
||||||
|
{
|
||||||
|
private MongoDbIndexHandler _mongoDbIndexHandler;
|
||||||
|
protected virtual MongoDbIndexHandler MongoDbIndexHandler
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_mongoDbIndexHandler != null) { return _mongoDbIndexHandler; }
|
||||||
|
|
||||||
|
lock (_initLock)
|
||||||
|
{
|
||||||
|
if (_mongoDbIndexHandler == null)
|
||||||
|
{
|
||||||
|
_mongoDbIndexHandler = new MongoDbIndexHandler(MongoDbContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return _mongoDbIndexHandler;
|
||||||
|
}
|
||||||
|
set { _mongoDbIndexHandler = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the names of the indexes present on a collection.
|
||||||
|
/// </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="partitionKey">An optional partition key</param>
|
||||||
|
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
|
||||||
|
public async Task<List<string>> GetIndexesNamesAsync<TDocument>(string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.GetIndexesNamesAsync<TDocument, Guid>(partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the names of the indexes present on a collection.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="partitionKey">An optional partition key</param>
|
||||||
|
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
|
||||||
|
public async virtual Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.GetIndexesNamesAsync<TDocument, TKey>(partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a text index on the given field.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async Task<string> CreateTextIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateTextIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a text index on the given field.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </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="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async virtual Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateTextIndexAsync<TDocument, TKey>(field, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an index on the given field in ascending order.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async Task<string> CreateAscendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateAscendingIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an index on the given field in ascending order.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </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="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async virtual Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateAscendingIndexAsync<TDocument, TKey>(field, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an index on the given field in descending order.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async Task<string> CreateDescendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateDescendingIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async virtual Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateDescendingIndexAsync<TDocument, TKey>(field, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a hashed index on the given field.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async Task<string> CreateHashedIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateHashedIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async virtual Task<string> CreateHashedIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateHashedIndexAsync<TDocument, TKey>(field, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a combined text index.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="fields">The fields we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async Task<string> CreateCombinedTextIndexAsync<TDocument>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateCombinedTextIndexAsync<TDocument, Guid>(fields, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async virtual Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateCombinedTextIndexAsync<TDocument, TKey>(fields, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Drops the index given a field name
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="indexName">The name of the index</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key</param>
|
||||||
|
public async Task DropIndexAsync<TDocument>(string indexName, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
await MongoDbIndexHandler.DropIndexAsync<TDocument, Guid>(indexName, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async virtual Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
await MongoDbIndexHandler.DropIndexAsync<TDocument, TKey>(indexName, partitionKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using MongoDbGenericRepository.Utils;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
|
||||||
|
/// Its constructor must be given a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
public abstract partial class BaseMongoRepository : ReadOnlyMongoRepository, IBaseMongoRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The constructor taking a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="connectionString">The connection string of the MongoDb server.</param>
|
||||||
|
/// <param name="databaseName">The name of the database against which you want to perform operations.</param>
|
||||||
|
protected BaseMongoRepository(string connectionString, string databaseName = null) : base(connectionString, databaseName)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The contructor taking a <see cref="IMongoDbContext"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
||||||
|
protected BaseMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The contructor taking a <see cref="IMongoDatabase"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
||||||
|
protected BaseMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression 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 virtual 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>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression 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 virtual async Task<List<TDocument>> GetPaginatedAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Find And Update
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// GetAndUpdateOne with filter
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="update"></param>
|
||||||
|
/// <param name="options"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<TDocument> GetAndUpdateOne<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options) where TDocument : IDocument
|
||||||
|
{
|
||||||
|
return await GetCollection<TDocument>().FindOneAndUpdateAsync(filter, update, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// GetAndUpdateOne with filter
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="update"></param>
|
||||||
|
/// <param name="options"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<TDocument> GetAndUpdateOne<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await GetCollection<TDocument, TKey>().FindOneAndUpdateAsync(filter, update, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Find And Update
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the value of the document Id if it is not set already.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="document">The document.</param>
|
||||||
|
protected void FormatDocument<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
if (document == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(document));
|
||||||
|
}
|
||||||
|
var defaultTKey = default(TKey);
|
||||||
|
if (document.Id == null
|
||||||
|
|| (defaultTKey != null
|
||||||
|
&& defaultTKey.Equals(document.Id)))
|
||||||
|
{
|
||||||
|
document.Id = IdGenerator.GetId<TKey>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the value of the document Id if it is not set already.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="document">The document.</param>
|
||||||
|
protected void FormatDocument<TDocument>(TDocument document) where TDocument : IDocument
|
||||||
|
{
|
||||||
|
if (document == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(document));
|
||||||
|
}
|
||||||
|
if (document.Id == default(Guid))
|
||||||
|
{
|
||||||
|
document.Id = Guid.NewGuid();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a collections for a potentially partitioned document type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="partitionKey">The collection partition key.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected virtual IMongoCollection<TDocument> HandlePartitioned<TDocument>(string partitionKey)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(partitionKey))
|
||||||
|
{
|
||||||
|
return GetCollection<TDocument>(partitionKey);
|
||||||
|
}
|
||||||
|
return GetCollection<TDocument>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a collections for the type TDocument with a partition key.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="partitionKey">The collection partition key.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected virtual IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbContext.GetCollection<TDocument>(partitionKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,197 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.DataAccess.Update;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
public abstract partial class BaseMongoRepository : IBaseMongoRepository_Update_ClientSession
|
||||||
|
{
|
||||||
|
/// <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="session">The client session.</param>
|
||||||
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey>(session, modifiedDocument, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="session">The client session.</param>
|
||||||
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey>(session, modifiedDocument, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="session">The client session.</param>
|
||||||
|
/// <param name="documentToModify">The document to modify.</param>
|
||||||
|
/// <param name="update">The update definition.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey>(session, documentToModify, update, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="session">The client session.</param>
|
||||||
|
/// <param name="documentToModify">The document to modify.</param>
|
||||||
|
/// <param name="update">The update definition.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey>(session, documentToModify, update, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="documentToModify">The document to modify.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(session, documentToModify, field, value, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="documentToModify">The document to modify.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(session, documentToModify, field, value, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="filter">The filter for the update.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="partitionKey">The optional partition key.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(session, filter, field, value, partitionKey, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="filter">The filter for the update.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="partitionKey">The optional partition key.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(session, filter, field, value, partitionKey, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="filter">The filter for the update.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="partitionKey">The optional partition key.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(session, filter, field, value, partitionKey, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="filter">The filter for the update.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="partitionKey">The optional partition key.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return UpdateOne<TDocument, TKey, TField>(session, Builders<TDocument>.Filter.Where(filter), field, value, partitionKey, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,573 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.DataAccess.Update;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
|
||||||
|
/// Its constructor must be given a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
public abstract partial class BaseMongoRepository : IBaseMongoRepository_Update
|
||||||
|
{
|
||||||
|
private MongoDbUpdater _mongoDbUpdater;
|
||||||
|
protected virtual MongoDbUpdater MongoDbUpdater
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_mongoDbUpdater != null) { return _mongoDbUpdater; }
|
||||||
|
|
||||||
|
lock (_initLock)
|
||||||
|
{
|
||||||
|
if (_mongoDbUpdater == null)
|
||||||
|
{
|
||||||
|
_mongoDbUpdater = new MongoDbUpdater(MongoDbContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return _mongoDbUpdater;
|
||||||
|
}
|
||||||
|
set { _mongoDbUpdater = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Update
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously Updates a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||||
|
public virtual async Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, Guid>(modifiedDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||||
|
public virtual bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, Guid>(modifiedDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <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>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, Guid>(documentToModify, update);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <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>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, Guid>(documentToModify, update);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="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, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, Guid, TField>(documentToModify, field, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="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, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, Guid, TField>(documentToModify, field, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="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, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, Guid, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="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, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, Guid, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="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, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, Guid, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="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, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, Guid, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, updates the property field with the given value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing 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<long> UpdateManyAsync<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateManyAsync<TDocument, Guid, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, updates the property field with the given value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing 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<long> UpdateManyAsync<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateManyAsync<TDocument, Guid, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, 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="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public virtual async Task<long> UpdateManyAsync<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateManyAsync<TDocument, Guid>(filter, updateDefinition, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, applies the update you have defined in MongoDb.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public async Task<long> UpdateManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateManyAsync<TDocument, Guid>(filter, updateDefinition, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, updates the property field with the given value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing 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 long UpdateMany<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateMany<TDocument, Guid, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, updates the property field with the given value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing 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 long UpdateMany<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateMany<TDocument, Guid, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, applies the update you have defined in MongoDb.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public virtual long UpdateMany<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateMany<TDocument, Guid>(filter, updateDefinition, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, applies the update you have defined in MongoDb.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public long UpdateMany<TDocument>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateMany<TDocument, Guid>(filter, updateDefinition, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Update
|
||||||
|
|
||||||
|
#region Update TKey
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey>(modifiedDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey>(modifiedDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey>(documentToModify, update);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey>(documentToModify, update);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(documentToModify, field, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(documentToModify, field, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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 MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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 MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities 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<long> UpdateManyAsync<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 MongoDbUpdater.UpdateManyAsync<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities 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 value of the partition key.</param>
|
||||||
|
public virtual async Task<long> UpdateManyAsync<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateManyAsync<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, 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="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public virtual async Task<long> UpdateManyAsync<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateManyAsync<TDocument, TKey>(filter, updateDefinition, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, 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="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public virtual async Task<long> UpdateManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateManyAsync<TDocument, TKey>(filter, updateDefinition, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities 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 long UpdateMany<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 MongoDbUpdater.UpdateMany<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities 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 value of the partition key.</param>
|
||||||
|
public virtual long UpdateMany<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateMany<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, 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="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public virtual long UpdateMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateMany<TDocument, TKey>(filter, updateDefinition, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, 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="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public virtual long UpdateMany<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateMany<TDocument, TKey>(filter, updateDefinition, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Update
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,158 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Driver.Linq;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository.DataAccess.Base
|
||||||
|
{
|
||||||
|
public class DataAccessBase
|
||||||
|
{
|
||||||
|
protected IMongoDbContext MongoDbContext;
|
||||||
|
|
||||||
|
public DataAccessBase(IMongoDbContext mongoDbContext)
|
||||||
|
{
|
||||||
|
MongoDbContext = mongoDbContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Utility Methods
|
||||||
|
|
||||||
|
public virtual IMongoQueryable<TDocument> GetQuery<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return GetCollection<TDocument, TKey>(partitionKey).AsQueryable().Where(filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a collections for a potentially partitioned document type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="document">The document.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual IMongoCollection<TDocument> HandlePartitioned<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
if (document is IPartitionedDocument)
|
||||||
|
{
|
||||||
|
return GetCollection<TDocument, TKey>(((IPartitionedDocument)document).PartitionKey);
|
||||||
|
}
|
||||||
|
return GetCollection<TDocument, TKey>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a collections for the type TDocument with a partition key.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="partitionKey">The collection partition key.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbContext.GetCollection<TDocument>(partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a collections for a potentially partitioned document type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="partitionKey">The collection partition key.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual IMongoCollection<TDocument> HandlePartitioned<TDocument, TKey>(string partitionKey)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(partitionKey))
|
||||||
|
{
|
||||||
|
return GetCollection<TDocument, TKey>(partitionKey);
|
||||||
|
}
|
||||||
|
return GetCollection<TDocument, TKey>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts a LINQ expression of TDocument, TValue to a LINQ expression of TDocument, object
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value.</typeparam>
|
||||||
|
/// <param name="expression">The expression to convert</param>
|
||||||
|
protected virtual Expression<Func<TDocument, object>> ConvertExpression<TDocument, TValue>(Expression<Func<TDocument, TValue>> expression)
|
||||||
|
{
|
||||||
|
var param = expression.Parameters[0];
|
||||||
|
Expression body = expression.Body;
|
||||||
|
var convert = Expression.Convert(body, typeof(object));
|
||||||
|
return Expression.Lambda<Func<TDocument, object>>(convert, param);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps a IndexCreationOptions object to a MongoDB.Driver.CreateIndexOptions object
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="indexCreationOptions">The options for creating an index.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected virtual CreateIndexOptions MapIndexOptions(IndexCreationOptions indexCreationOptions)
|
||||||
|
{
|
||||||
|
return new CreateIndexOptions
|
||||||
|
{
|
||||||
|
Unique = indexCreationOptions.Unique,
|
||||||
|
TextIndexVersion = indexCreationOptions.TextIndexVersion,
|
||||||
|
SphereIndexVersion = indexCreationOptions.SphereIndexVersion,
|
||||||
|
Sparse = indexCreationOptions.Sparse,
|
||||||
|
Name = indexCreationOptions.Name,
|
||||||
|
Min = indexCreationOptions.Min,
|
||||||
|
Max = indexCreationOptions.Max,
|
||||||
|
LanguageOverride = indexCreationOptions.LanguageOverride,
|
||||||
|
ExpireAfter = indexCreationOptions.ExpireAfter,
|
||||||
|
DefaultLanguage = indexCreationOptions.DefaultLanguage,
|
||||||
|
BucketSize = indexCreationOptions.BucketSize,
|
||||||
|
Bits = indexCreationOptions.Bits,
|
||||||
|
Background = indexCreationOptions.Background,
|
||||||
|
Version = indexCreationOptions.Version
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
protected virtual IFindFluent<TDocument, TDocument> GetMinMongoQuery<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.SortBy(ConvertExpression(minValueSelector))
|
||||||
|
.Limit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
protected virtual IFindFluent<TDocument, TDocument> GetMaxMongoQuery<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.SortByDescending(ConvertExpression(maxValueSelector))
|
||||||
|
.Limit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Driver.Linq;
|
||||||
|
using MongoDbGenericRepository.DataAccess.Base;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using MongoDbGenericRepository.Utils;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository.DataAccess.Create
|
||||||
|
{
|
||||||
|
public class MongoDbCreator : DataAccessBase
|
||||||
|
{
|
||||||
|
public MongoDbCreator(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Create TKey
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously adds a document to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </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="document">The document you want to add.</param>
|
||||||
|
public virtual async Task AddOneAsync<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
FormatDocument<TDocument, TKey>(document);
|
||||||
|
await HandlePartitioned<TDocument, TKey>(document).InsertOneAsync(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a document to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </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="document">The document you want to add.</param>
|
||||||
|
public virtual void AddOne<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
FormatDocument<TDocument, TKey>(document);
|
||||||
|
HandlePartitioned<TDocument, TKey>(document).InsertOne(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously adds a list of documents to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </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="documents">The documents you want to add.</param>
|
||||||
|
public virtual async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
if (!documents.Any())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
foreach (var document in documents)
|
||||||
|
{
|
||||||
|
FormatDocument<TDocument, TKey>(document);
|
||||||
|
}
|
||||||
|
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||||
|
if (documents.Any(e => e is IPartitionedDocument))
|
||||||
|
{
|
||||||
|
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||||
|
{
|
||||||
|
await HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).InsertManyAsync(group.ToList());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await GetCollection<TDocument, TKey>().InsertManyAsync(documents.ToList());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a list of documents to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </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="documents">The documents you want to add.</param>
|
||||||
|
public virtual void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
if (!documents.Any())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
foreach (var document in documents)
|
||||||
|
{
|
||||||
|
FormatDocument<TDocument, TKey>(document);
|
||||||
|
}
|
||||||
|
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||||
|
if (documents.Any(e => e is IPartitionedDocument))
|
||||||
|
{
|
||||||
|
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||||
|
{
|
||||||
|
HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).InsertMany(group.ToList());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
GetCollection<TDocument, TKey>().InsertMany(documents.ToList());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the value of the document Id if it is not set already.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="document">The document.</param>
|
||||||
|
protected void FormatDocument<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
if (document == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(document));
|
||||||
|
}
|
||||||
|
var defaultTKey = default(TKey);
|
||||||
|
if (document.Id == null
|
||||||
|
|| (defaultTKey != null
|
||||||
|
&& defaultTKey.Equals(document.Id)))
|
||||||
|
{
|
||||||
|
document.Id = IdGenerator.GetId<TKey>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,180 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes 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="document">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual long DeleteOne<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var filter = Builders<TDocument>.Filter.Eq("Id", document.Id);
|
||||||
|
return HandlePartitioned<TDocument, TKey>(document).DeleteOne(filter).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="document">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var filter = Builders<TDocument>.Filter.Eq("Id", document.Id);
|
||||||
|
return (await HandlePartitioned<TDocument, TKey>(document).DeleteOneAsync(filter)).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument, TKey>(partitionKey).DeleteOne(filter).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return (await HandlePartitioned<TDocument, TKey>(partitionKey).DeleteOneAsync(filter)).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return (await HandlePartitioned<TDocument, TKey>(partitionKey).DeleteManyAsync(filter)).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a list of documents.
|
||||||
|
/// </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="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
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<TDocument, TKey>(group.FirstOrDefault()).DeleteManyAsync(x => groupIdsTodelete.Contains(x.Id))).DeletedCount;
|
||||||
|
}
|
||||||
|
return deleteCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var idsTodelete = documents.Select(e => e.Id).ToArray();
|
||||||
|
return (await HandlePartitioned<TDocument, TKey>(documents.FirstOrDefault()).DeleteManyAsync(x => idsTodelete.Contains(x.Id))).DeletedCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a list of documents.
|
||||||
|
/// </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="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
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<TDocument, TKey>(group.FirstOrDefault()).DeleteMany(x => groupIdsTodelete.Contains(x.Id))).DeletedCount;
|
||||||
|
}
|
||||||
|
return deleteCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var idsTodelete = documents.Select(e => e.Id).ToArray();
|
||||||
|
return (HandlePartitioned<TDocument, TKey>(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">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key for a Document.</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 virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument, TKey>(partitionKey).DeleteMany(filter).DeletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
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.Index
|
||||||
|
{
|
||||||
|
public class MongoDbIndexHandler : DataAccessBase
|
||||||
|
{
|
||||||
|
public MongoDbIndexHandler(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the names of the indexes present on a collection.
|
||||||
|
/// </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="partitionKey">An optional partition key</param>
|
||||||
|
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
|
||||||
|
public async virtual Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var indexCursor = await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.ListAsync();
|
||||||
|
var indexes = await indexCursor.ToListAsync();
|
||||||
|
return indexes.Select(e => e["name"].ToString()).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a text index on the given field.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </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="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async virtual Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes
|
||||||
|
.CreateOneAsync(
|
||||||
|
new CreateIndexModel<TDocument>(
|
||||||
|
Builders<TDocument>.IndexKeys.Text(field),
|
||||||
|
indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an index on the given field in ascending order.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </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="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async virtual Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||||
|
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||||
|
var indexKey = Builders<TDocument>.IndexKeys;
|
||||||
|
return await collection.Indexes
|
||||||
|
.CreateOneAsync(
|
||||||
|
new CreateIndexModel<TDocument>(indexKey.Ascending(field), createOptions));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an index on the given field in descending order.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </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="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async virtual Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||||
|
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||||
|
var indexKey = Builders<TDocument>.IndexKeys;
|
||||||
|
return await collection.Indexes
|
||||||
|
.CreateOneAsync(
|
||||||
|
new CreateIndexModel<TDocument>(indexKey.Descending(field), createOptions));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a hashed index on the given field.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </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="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async virtual Task<string> CreateHashedIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||||
|
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||||
|
var indexKey = Builders<TDocument>.IndexKeys;
|
||||||
|
return await collection.Indexes
|
||||||
|
.CreateOneAsync(
|
||||||
|
new CreateIndexModel<TDocument>(indexKey.Hashed(field), createOptions));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a combined text index.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </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="fields">The fields we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async virtual Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||||
|
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||||
|
var listOfDefs = new List<IndexKeysDefinition<TDocument>>();
|
||||||
|
foreach (var field in fields)
|
||||||
|
{
|
||||||
|
listOfDefs.Add(Builders<TDocument>.IndexKeys.Text(field));
|
||||||
|
}
|
||||||
|
return await collection.Indexes
|
||||||
|
.CreateOneAsync(new CreateIndexModel<TDocument>(Builders<TDocument>.IndexKeys.Combine(listOfDefs), createOptions));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Drops the index given a field name
|
||||||
|
/// </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="indexName">The name of the index</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key</param>
|
||||||
|
public async virtual Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.DropOneAsync(indexName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,514 @@
|
|||||||
|
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.Read
|
||||||
|
{
|
||||||
|
public partial class MongoDbReader : DataAccessBase
|
||||||
|
{
|
||||||
|
public MongoDbReader(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Read TKey
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns one document given its id.
|
||||||
|
/// </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="id">The Id of the document you want to get.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public async virtual Task<TDocument> GetByIdAsync<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
||||||
|
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns one document given its id.
|
||||||
|
/// </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="id">The Id of the document you want to get.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual TDocument GetById<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
||||||
|
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns one document given an expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public async virtual Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns one document given an expression filter.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a collection cursor.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if any of the document of the collection matches the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public async virtual Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var count = await HandlePartitioned<TDocument, TKey>(partitionKey).CountDocumentsAsync(filter);
|
||||||
|
return (count > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if any of the document of the collection matches the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var count = HandlePartitioned<TDocument, TKey>(partitionKey).CountDocuments(filter);
|
||||||
|
return (count > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public async virtual Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously counts how many documents match the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey</param>
|
||||||
|
public async virtual Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await HandlePartitioned<TDocument, TKey>(partitionKey).CountDocumentsAsync(filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Counts how many documents match the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey</param>
|
||||||
|
public virtual long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).CountDocuments();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Min / Max
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
public async virtual Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.SortByDescending(maxValueSelector)
|
||||||
|
.Limit(1)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
public virtual TDocument GetByMax<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.SortByDescending(maxValueSelector)
|
||||||
|
.Limit(1)
|
||||||
|
.FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
public async virtual Task<TDocument> GetByMinAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.SortBy(minValueSelector)
|
||||||
|
.Limit(1)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
public virtual TDocument GetByMin<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.SortBy(minValueSelector)
|
||||||
|
.Limit(1)
|
||||||
|
.FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
public async virtual Task<TValue> GetMaxValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await GetMaxMongoQuery<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey)
|
||||||
|
.Project(maxValueSelector)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
public virtual TValue GetMaxValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return GetMaxMongoQuery<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey)
|
||||||
|
.Project(maxValueSelector)
|
||||||
|
.FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual async Task<TValue> GetMinValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await GetMinMongoQuery<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual TValue GetMinValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return GetMinMongoQuery<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion Min / Max
|
||||||
|
|
||||||
|
#region Sum TKey
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual async Task<int> SumByAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, int>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await GetQuery<TDocument, TKey>(filter, partitionKey).SumAsync(selector);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual int SumBy<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, int>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return GetQuery<TDocument, TKey>(filter, partitionKey).Sum(selector);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual async Task<decimal> SumByAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, decimal>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await GetQuery<TDocument, TKey>(filter, partitionKey).SumAsync(selector);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual decimal SumBy<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, decimal>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return GetQuery<TDocument, TKey>(filter, partitionKey).Sum(selector);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Sum TKey
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class MongoDbReader
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Groups a collection of documents given a grouping criteria,
|
||||||
|
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
|
||||||
|
/// </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="TGroupKey">The type of the grouping criteria.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
||||||
|
/// <param name="groupingCriteria">The grouping criteria.</param>
|
||||||
|
/// <param name="groupProjection">The projected group result.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual List<TProjection> GroupBy<TDocument, TGroupKey, TProjection, TKey>(
|
||||||
|
Expression<Func<TDocument, TGroupKey>> groupingCriteria,
|
||||||
|
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class, new()
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument, TKey>(partitionKey)
|
||||||
|
.Aggregate()
|
||||||
|
.Group(groupingCriteria, groupProjection)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Groups filtered a collection of documents given a grouping criteria,
|
||||||
|
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
|
||||||
|
/// </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="TGroupKey">The type of the grouping criteria.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The grouping criteria.</param>
|
||||||
|
/// <param name="projection">The projected group result.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual List<TProjection> GroupBy<TDocument, TGroupKey, TProjection, TKey>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, TGroupKey>> selector,
|
||||||
|
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> projection,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class, new()
|
||||||
|
{
|
||||||
|
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||||
|
return collection.Aggregate()
|
||||||
|
.Match(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.Group(selector, projection)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="sortSelector">The property selector.</param>
|
||||||
|
/// <param name="ascending">Order of the sorting.</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 virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument, TKey>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, object>> sortSelector,
|
||||||
|
bool ascending = true,
|
||||||
|
int skipNumber = 0,
|
||||||
|
int takeNumber = 50,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var sorting = ascending
|
||||||
|
? Builders<TDocument>.Sort.Ascending(sortSelector)
|
||||||
|
: Builders<TDocument>.Sort.Descending(sortSelector);
|
||||||
|
|
||||||
|
return await HandlePartitioned<TDocument, TKey>(partitionKey)
|
||||||
|
.Find(filter)
|
||||||
|
.Sort(sorting)
|
||||||
|
.Skip(skipNumber)
|
||||||
|
.Limit(takeNumber)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="sortDefinition">The sort definition.</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 virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument, TKey>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
SortDefinition<TDocument> sortDefinition,
|
||||||
|
int skipNumber = 0,
|
||||||
|
int takeNumber = 50,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await HandlePartitioned<TDocument, TKey>(partitionKey)
|
||||||
|
.Find(filter)
|
||||||
|
.Sort(sortDefinition)
|
||||||
|
.Skip(skipNumber)
|
||||||
|
.Limit(takeNumber)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Driver.Linq;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository.DataAccess.Read
|
||||||
|
{
|
||||||
|
public partial class MongoDbReader
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a projected document matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual async Task<TProjection> ProjectOneAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter)
|
||||||
|
.Project(projection)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a projected document matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual TProjection ProjectOne<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter)
|
||||||
|
.Project(projection)
|
||||||
|
.FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual async Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter)
|
||||||
|
.Project(projection)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual List<TProjection> ProjectMany<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter)
|
||||||
|
.Project(projection)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,217 @@
|
|||||||
|
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
|
||||||
|
{
|
||||||
|
/// <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="session">The client session.</param>
|
||||||
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
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(session, filter, modifiedDocument, cancellationToken: cancellationToken)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
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="session">The client session.</param>
|
||||||
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var filter = Builders<TDocument>.Filter.Eq("Id", modifiedDocument.Id);
|
||||||
|
var updateRes = HandlePartitioned<TDocument, TKey>(modifiedDocument).ReplaceOne(session, filter, modifiedDocument, cancellationToken: cancellationToken);
|
||||||
|
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="session">The client session.</param>
|
||||||
|
/// <param name="documentToModify">The document to modify.</param>
|
||||||
|
/// <param name="update">The update definition.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
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(session, filter, update, new UpdateOptions { IsUpsert = true }, cancellationToken).ConfigureAwait(false);
|
||||||
|
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="session">The client session.</param>
|
||||||
|
/// <param name="documentToModify">The document to modify.</param>
|
||||||
|
/// <param name="update">The update definition.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||||
|
var updateRes = HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOne(session, filter, update, new UpdateOptions { IsUpsert = true }, cancellationToken);
|
||||||
|
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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="documentToModify">The document to modify.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
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(session, filter, Builders<TDocument>.Update.Set(field, value), cancellationToken: cancellationToken)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="documentToModify">The document to modify.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||||
|
var updateRes = HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOne(session, filter, Builders<TDocument>.Update.Set(field, value), cancellationToken: cancellationToken);
|
||||||
|
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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="filter">The filter for the update.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="partitionKey">The optional partition key.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
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(session, filter, Builders<TDocument>.Update.Set(field, value), cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||||
|
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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="filter">The filter for the update.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="partitionKey">The optional partition key.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return UpdateOneAsync<TDocument, TKey, TField>(session, Builders<TDocument>.Filter.Where(filter), field, value, partitionKey, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="filter">The filter for the update.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="partitionKey">The optional partition key.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
|
||||||
|
var updateRes = collection.UpdateOne(session, filter, Builders<TDocument>.Update.Set(field, value), cancellationToken: cancellationToken);
|
||||||
|
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>
|
||||||
|
/// <typeparam name="TField">The type of the field to update.</typeparam>
|
||||||
|
/// <param name="session">The client session.</param>
|
||||||
|
/// <param name="filter">The filter for the update.</param>
|
||||||
|
/// <param name="field">The field to update.</param>
|
||||||
|
/// <param name="value">The value of the field.</param>
|
||||||
|
/// <param name="partitionKey">The optional partition key.</param>
|
||||||
|
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return UpdateOne<TDocument, TKey, TField>(session, Builders<TDocument>.Filter.Where(filter), field, value, partitionKey, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,311 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities 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<long> UpdateManyAsync<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 UpdateManyAsync<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities 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 value of the partition key.</param>
|
||||||
|
public virtual async Task<long> UpdateManyAsync<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.UpdateManyAsync(filter, Builders<TDocument>.Update.Set(field, value));
|
||||||
|
return updateRes.ModifiedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, apply the update definition.
|
||||||
|
/// </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="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<long> UpdateManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await UpdateManyAsync<TDocument, TKey>(Builders<TDocument>.Filter.Where(filter), update, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, apply the update definition.
|
||||||
|
/// </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="updateDefinition">The update definition.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public virtual async Task<long> UpdateManyAsync<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, 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.UpdateManyAsync(filter, updateDefinition, new UpdateOptions { IsUpsert = true });
|
||||||
|
return updateRes.ModifiedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities 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 long UpdateMany<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 UpdateMany<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities 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 value of the partition key.</param>
|
||||||
|
public virtual long UpdateMany<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.UpdateMany(filter, Builders<TDocument>.Update.Set(field, value));
|
||||||
|
return updateRes.ModifiedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, apply the update definition.
|
||||||
|
/// </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="UpdateDefinition">The update definition.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public virtual long UpdateMany<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> UpdateDefinition, 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.UpdateMany(filter, UpdateDefinition, new UpdateOptions { IsUpsert = true });
|
||||||
|
return updateRes.ModifiedCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
using MongoDbGenericRepository.DataAccess.Create;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The interface exposing data insertion functionality for Key typed repositories.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TKey"></typeparam>
|
||||||
|
public interface IBaseMongoRepository_Create<TKey> where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously adds a document to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="document">The document you want to add.</param>
|
||||||
|
Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a document to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="document">The document you want to add.</param>
|
||||||
|
void AddOne<TDocument>(TDocument document) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously adds a list of documents to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="documents">The documents you want to add.</param>
|
||||||
|
Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a list of documents to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="documents">The documents you want to add.</param>
|
||||||
|
void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<TKey>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
|
||||||
|
/// Its constructor must be given a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
public abstract partial class BaseMongoRepository<TKey> : IBaseMongoRepository_Create<TKey> where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
private MongoDbCreator _mongoDbCreator;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The MongoDb accessor to insert data.
|
||||||
|
/// </summary>
|
||||||
|
protected virtual MongoDbCreator MongoDbCreator
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if(_mongoDbCreator == null)
|
||||||
|
{
|
||||||
|
lock (_initLock)
|
||||||
|
{
|
||||||
|
if(_mongoDbCreator == null)
|
||||||
|
{
|
||||||
|
_mongoDbCreator = new MongoDbCreator(MongoDbContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return _mongoDbCreator;
|
||||||
|
}
|
||||||
|
set { _mongoDbCreator = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously adds a document to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="document">The document you want to add.</param>
|
||||||
|
public virtual async Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
await MongoDbCreator.AddOneAsync<TDocument, TKey>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a document to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="document">The document you want to add.</param>
|
||||||
|
public virtual void AddOne<TDocument>(TDocument document) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
MongoDbCreator.AddOne<TDocument, TKey>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously adds a list of documents to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="documents">The documents you want to add.</param>
|
||||||
|
public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
await MongoDbCreator.AddManyAsync<TDocument, TKey>(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a list of documents to the collection.
|
||||||
|
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="documents">The documents you want to add.</param>
|
||||||
|
public virtual void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
MongoDbCreator.AddMany<TDocument, TKey>(documents);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,219 @@
|
|||||||
|
using MongoDbGenericRepository.DataAccess.Delete;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The interface exposing deletion functionality for Key typed repositories.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TKey">The type of the document Id.</typeparam>
|
||||||
|
public interface IBaseMongoRepository_Delete<TKey> where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="document">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
long DeleteOne<TDocument>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="document">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
Task<long> DeleteOneAsync<TDocument>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a list of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a list of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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<TKey>;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract partial class BaseMongoRepository<TKey>: IBaseMongoRepository_Delete<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
private MongoDbEraser _mongoDbEraser;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The MongoDb accessor to delete data.
|
||||||
|
/// </summary>
|
||||||
|
protected virtual MongoDbEraser MongoDbEraser
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_mongoDbEraser != null) { return _mongoDbEraser; }
|
||||||
|
|
||||||
|
lock (_initLock)
|
||||||
|
{
|
||||||
|
if (_mongoDbEraser == null)
|
||||||
|
{
|
||||||
|
_mongoDbEraser = new MongoDbEraser(MongoDbContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return _mongoDbEraser;
|
||||||
|
}
|
||||||
|
set { _mongoDbEraser = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="document">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual long DeleteOne<TDocument>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbEraser.DeleteOne<TDocument, TKey>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="document">The document you want to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual async Task<long> DeleteOneAsync<TDocument>(TDocument document)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbEraser.DeleteOneAsync<TDocument, TKey>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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 virtual long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbEraser.DeleteOne<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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 virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbEraser.DeleteOneAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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 virtual async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbEraser.DeleteManyAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes a list of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbEraser.DeleteManyAsync<TDocument, TKey>(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a list of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="documents">The list of documents to delete.</param>
|
||||||
|
/// <returns>The number of documents deleted.</returns>
|
||||||
|
public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbEraser.DeleteMany<TDocument, TKey>(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes the documents matching the condition of the LINQ expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</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 virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbEraser.DeleteMany<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+150
-109
@@ -1,4 +1,5 @@
|
|||||||
using MongoDbGenericRepository.Models;
|
using MongoDbGenericRepository.DataAccess.Index;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
@@ -7,10 +8,20 @@ using System.Threading.Tasks;
|
|||||||
namespace MongoDbGenericRepository
|
namespace MongoDbGenericRepository
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The repository interface for managing indexes
|
/// The interface exposing index management functionality for Key typed repositories.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IMongoDbCollectionIndexRepository
|
/// <typeparam name="TKey"></typeparam>
|
||||||
|
public interface IBaseMongoRepository_Index<TKey> where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the names of the indexes present on a collection.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="partitionKey">An optional partition key</param>
|
||||||
|
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
|
||||||
|
Task<List<string>> GetIndexesNamesAsync<TDocument>(string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a text index on the given field.
|
/// Create a text index on the given field.
|
||||||
/// IndexCreationOptions can be supplied to further specify
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
@@ -22,22 +33,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
/// <returns>The result of the create index operation.</returns>
|
/// <returns>The result of the create index operation.</returns>
|
||||||
Task<string> CreateTextIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
Task<string> CreateTextIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
where TDocument : IDocument;
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Create a text index on the given field.
|
|
||||||
/// IndexCreationOptions can be supplied to further specify
|
|
||||||
/// how the creation should be done.
|
|
||||||
/// </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="field">The field we want to index.</param>
|
|
||||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
/// <returns>The result of the create index operation.</returns>
|
|
||||||
Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an index on the given field in ascending order.
|
/// Creates an index on the given field in ascending order.
|
||||||
@@ -49,22 +45,8 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
/// <returns>The result of the create index operation.</returns>
|
/// <returns>The result of the create index operation.</returns>
|
||||||
Task<string> CreateAscendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument;
|
Task<string> CreateAscendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
/// <summary>
|
|
||||||
/// Creates an index on the given field in ascending order.
|
|
||||||
/// IndexCreationOptions can be supplied to further specify
|
|
||||||
/// how the creation should be done.
|
|
||||||
/// </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="field">The field we want to index.</param>
|
|
||||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
/// <returns>The result of the create index operation.</returns>
|
|
||||||
Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an index on the given field in descending order.
|
/// Creates an index on the given field in descending order.
|
||||||
@@ -75,24 +57,9 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="field">The field we want to index.</param>
|
/// <param name="field">The field we want to index.</param>
|
||||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
/// <returns>A string containing the result of the operation.</returns>
|
/// <returns>The result of the create index operation.</returns>
|
||||||
Task<string> CreateDescendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
Task<string> CreateDescendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
where TDocument : IDocument;
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an index on the given field in descending order.
|
|
||||||
/// IndexCreationOptions can be supplied to further specify
|
|
||||||
/// how the creation should be done.
|
|
||||||
/// </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="field">The field we want to index.</param>
|
|
||||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
/// <returns>The result of the create index operation.</returns>
|
|
||||||
Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a hashed index on the given field.
|
/// Creates a hashed index on the given field.
|
||||||
@@ -103,24 +70,9 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="field">The field we want to index.</param>
|
/// <param name="field">The field we want to index.</param>
|
||||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
/// <returns>A string containing the result of the operation.</returns>
|
/// <returns>The result of the create index operation.</returns>
|
||||||
Task<string> CreateHashedIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
Task<string> CreateHashedIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
where TDocument : IDocument;
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a hashed index on the given field.
|
|
||||||
/// IndexCreationOptions can be supplied to further specify
|
|
||||||
/// how the creation should be done.
|
|
||||||
/// </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="field">The field we want to index.</param>
|
|
||||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
/// <returns>The result of the create index operation.</returns>
|
|
||||||
Task<string> CreateHashedIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a combined text index.
|
/// Creates a combined text index.
|
||||||
@@ -133,22 +85,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
/// <returns>The result of the create index operation.</returns>
|
/// <returns>The result of the create index operation.</returns>
|
||||||
Task<string> CreateCombinedTextIndexAsync<TDocument>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
Task<string> CreateCombinedTextIndexAsync<TDocument>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
where TDocument : IDocument;
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a combined text index.
|
|
||||||
/// IndexCreationOptions can be supplied to further specify
|
|
||||||
/// how the creation should be done.
|
|
||||||
/// </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="fields">The fields we want to index.</param>
|
|
||||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
/// <returns>The result of the create index operation.</returns>
|
|
||||||
Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Drops the index given a field name
|
/// Drops the index given a field name
|
||||||
@@ -157,37 +94,141 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="indexName">The name of the index</param>
|
/// <param name="indexName">The name of the index</param>
|
||||||
/// <param name="partitionKey">An optional partition key</param>
|
/// <param name="partitionKey">An optional partition key</param>
|
||||||
Task DropIndexAsync<TDocument>(string indexName, string partitionKey = null)
|
Task DropIndexAsync<TDocument>(string indexName, string partitionKey = null)
|
||||||
where TDocument : IDocument;
|
where TDocument : IDocument<TKey>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
|
||||||
|
/// Its constructor must be given a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
public abstract partial class BaseMongoRepository<TKey> : IBaseMongoRepository_Index<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
private MongoDbIndexHandler _mongoDbIndexHandler;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The MongoDb accessor to manage indexes.
|
||||||
|
/// </summary>
|
||||||
|
protected virtual MongoDbIndexHandler MongoDbIndexHandler
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_mongoDbIndexHandler != null) { return _mongoDbIndexHandler; }
|
||||||
|
|
||||||
|
lock (_initLock)
|
||||||
|
{
|
||||||
|
if (_mongoDbIndexHandler == null)
|
||||||
|
{
|
||||||
|
_mongoDbIndexHandler = new MongoDbIndexHandler(MongoDbContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return _mongoDbIndexHandler;
|
||||||
|
}
|
||||||
|
set { _mongoDbIndexHandler = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the names of the indexes present on a collection.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="partitionKey">An optional partition key</param>
|
||||||
|
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
|
||||||
|
public async virtual Task<List<string>> GetIndexesNamesAsync<TDocument>(string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.GetIndexesNamesAsync<TDocument, TKey>(partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a text index on the given field.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async virtual Task<string> CreateTextIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateTextIndexAsync<TDocument, TKey>(field, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an index on the given field in ascending order.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async virtual Task<string> CreateAscendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateAscendingIndexAsync<TDocument, TKey>(field, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an index on the given field in descending order.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async virtual Task<string> CreateDescendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateDescendingIndexAsync<TDocument, TKey>(field, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a hashed index on the given field.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="field">The field we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async virtual Task<string> CreateHashedIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateHashedIndexAsync<TDocument, TKey>(field, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a combined text index.
|
||||||
|
/// IndexCreationOptions can be supplied to further specify
|
||||||
|
/// how the creation should be done.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="fields">The fields we want to index.</param>
|
||||||
|
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
/// <returns>The result of the create index operation.</returns>
|
||||||
|
public async virtual Task<string> CreateCombinedTextIndexAsync<TDocument>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbIndexHandler.CreateCombinedTextIndexAsync<TDocument, TKey>(fields, indexCreationOptions, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Drops the index given a field name
|
/// Drops the index given a field name
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
||||||
/// <param name="indexName">The name of the index</param>
|
/// <param name="indexName">The name of the index</param>
|
||||||
/// <param name="partitionKey">An optional partition key</param>
|
/// <param name="partitionKey">An optional partition key</param>
|
||||||
Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null)
|
public async virtual Task DropIndexAsync<TDocument>(string indexName, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>;
|
{
|
||||||
|
await MongoDbIndexHandler.DropIndexAsync<TDocument, TKey>(indexName, partitionKey);
|
||||||
/// <summary>
|
}
|
||||||
/// Returns the names of the indexes present on a collection.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="partitionKey">An optional partition key</param>
|
|
||||||
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
|
|
||||||
Task<List<string>> GetIndexesNamesAsync<TDocument>(string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the names of the indexes present on a collection.
|
|
||||||
/// </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="partitionKey">An optional partition key</param>
|
|
||||||
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
|
|
||||||
Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The interface exposing all the CRUD and Index functionalities for Key typed repositories.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TKey">The type of the document Id.</typeparam>
|
||||||
|
public interface IBaseMongoRepository<TKey> :
|
||||||
|
IReadOnlyMongoRepository<TKey>,
|
||||||
|
IBaseMongoRepository_Create<TKey>,
|
||||||
|
IBaseMongoRepository_Delete<TKey>,
|
||||||
|
IBaseMongoRepository_Index<TKey>,
|
||||||
|
IBaseMongoRepository_Update<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
|
||||||
|
/// Its constructor must be given a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TKey">The type of the document Id.</typeparam>
|
||||||
|
public abstract partial class BaseMongoRepository<TKey> :
|
||||||
|
ReadOnlyMongoRepository<TKey>,
|
||||||
|
IBaseMongoRepository<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
|
||||||
|
private readonly object _initLock = new object();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The constructor taking a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="connectionString">The connection string of the MongoDb server.</param>
|
||||||
|
/// <param name="databaseName">The name of the database against which you want to perform operations.</param>
|
||||||
|
protected BaseMongoRepository(string connectionString, string databaseName = null) : base(connectionString, databaseName)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The contructor taking a <see cref="IMongoDbContext"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
||||||
|
protected BaseMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The contructor taking a <see cref="IMongoDatabase"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
||||||
|
protected BaseMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a collections for a potentially partitioned document type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="partitionKey">The collection partition key.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected virtual IMongoCollection<TDocument> HandlePartitioned<TDocument>(string partitionKey)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(partitionKey))
|
||||||
|
{
|
||||||
|
return GetCollection<TDocument>(partitionKey);
|
||||||
|
}
|
||||||
|
return GetCollection<TDocument>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a collections for the type TDocument with a partition key.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="partitionKey">The collection partition key.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected virtual IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbContext.GetCollection<TDocument>(partitionKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,531 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.DataAccess.Read;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
|
||||||
|
/// Its constructor must be given a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
public abstract partial class ReadOnlyMongoRepository<TKey> : IReadOnlyMongoRepository<TKey> where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The connection string.
|
||||||
|
/// </summary>
|
||||||
|
public string ConnectionString { get; protected set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The database name.
|
||||||
|
/// </summary>
|
||||||
|
public string DatabaseName { get; protected set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The MongoDbContext
|
||||||
|
/// </summary>
|
||||||
|
protected IMongoDbContext MongoDbContext = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A MongoDb Reader for read operations
|
||||||
|
/// </summary>
|
||||||
|
protected MongoDbReader MongoDbReader = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The constructor taking a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="connectionString">The connection string of the MongoDb server.</param>
|
||||||
|
/// <param name="databaseName">The name of the database against which you want to perform operations.</param>
|
||||||
|
protected ReadOnlyMongoRepository(string connectionString, string databaseName = null)
|
||||||
|
{
|
||||||
|
SetupMongoDbContext(connectionString, databaseName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The contructor taking a <see cref="IMongoDatabase"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
||||||
|
protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : this(new MongoDbContext(mongoDatabase))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The contructor taking a <see cref="IMongoDbContext"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
||||||
|
protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext)
|
||||||
|
{
|
||||||
|
SetupMongoDbContext(mongoDbContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void SetupMongoDbContext(IMongoDbContext mongoDbContext)
|
||||||
|
{
|
||||||
|
MongoDbContext = MongoDbContext ?? mongoDbContext;
|
||||||
|
MongoDbReader = MongoDbReader ?? new MongoDbReader(MongoDbContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void SetupMongoDbContext(string connectionString, string databaseName)
|
||||||
|
{
|
||||||
|
if (databaseName == null)
|
||||||
|
{
|
||||||
|
var mongoUrl = new MongoUrl(connectionString);
|
||||||
|
databaseName = databaseName ?? mongoUrl.DatabaseName;
|
||||||
|
}
|
||||||
|
ConnectionString = connectionString;
|
||||||
|
DatabaseName = databaseName;
|
||||||
|
SetupMongoDbContext(new MongoDbContext(connectionString, databaseName));
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Read
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns one document given its id.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="id">The Id of the document you want to get.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public async Task<TDocument> GetByIdAsync<TDocument>(TKey id, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.GetByIdAsync<TDocument, TKey>(id, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns one document given its id.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="id">The Id of the document you want to get.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public TDocument GetById<TDocument>(TKey id, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.GetById<TDocument, TKey>(id, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns one document given an expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public async Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.GetOneAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns one document given an expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.GetOne<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a collection cursor.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.GetCursor<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if any of the document of the collection matches the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public async Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.AnyAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if any of the document of the collection matches the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.Any<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of the documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public async Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.GetAllAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a list of the documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.GetAll<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously counts how many documents match the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey</param>
|
||||||
|
public async Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.CountAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Counts how many documents match the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey</param>
|
||||||
|
public long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.Count<TDocument, TKey>(filter, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
public async Task<TDocument> GetByMaxAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.GetByMaxAsync<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public TDocument GetByMax<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.GetByMax<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
public async Task<TDocument> GetByMinAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.GetByMinAsync<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
public TDocument GetByMin<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.GetByMin<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
public async Task<TValue> GetMaxValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.GetMaxValueAsync<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
public TValue GetMaxValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.GetMaxValue<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual async Task<TValue> GetMinValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.GetMinValueAsync<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual TValue GetMinValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.GetMinValue<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Maths
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual async Task<int> SumByAsync<TDocument>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, int>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.SumByAsync<TDocument, TKey>(filter, selector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual async Task<decimal> SumByAsync<TDocument>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, decimal>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.SumByAsync<TDocument, TKey>(filter, selector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual int SumBy<TDocument>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, int>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.SumBy<TDocument, TKey>(filter, selector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual decimal SumBy<TDocument>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, decimal>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.SumBy<TDocument, TKey>(filter, selector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Maths
|
||||||
|
|
||||||
|
#region Project
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a projected document matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual async Task<TProjection> ProjectOneAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return await MongoDbReader.ProjectOneAsync<TDocument, TProjection, TKey>(filter, projection, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a projected document matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual TProjection ProjectOne<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return MongoDbReader.ProjectOne<TDocument, TProjection, TKey>(filter, projection, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual async Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return await MongoDbReader.ProjectManyAsync<TDocument, TProjection, TKey>(filter, projection, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual List<TProjection> ProjectMany<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return MongoDbReader.ProjectMany<TDocument, TProjection, TKey>(filter, projection, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion Project
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Groups a collection of documents given a grouping criteria,
|
||||||
|
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
||||||
|
/// <param name="groupingCriteria">The grouping criteria.</param>
|
||||||
|
/// <param name="groupProjection">The projected group result.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual List<TProjection> GroupBy<TDocument, TGroupKey, TProjection>(
|
||||||
|
Expression<Func<TDocument, TGroupKey>> groupingCriteria,
|
||||||
|
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TProjection : class, new()
|
||||||
|
{
|
||||||
|
return MongoDbReader.GroupBy<TDocument, TGroupKey, TProjection, TKey>(groupingCriteria, groupProjection, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Groups filtered a collection of documents given a grouping criteria,
|
||||||
|
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="groupingCriteria">The grouping criteria.</param>
|
||||||
|
/// <param name="groupProjection">The projected group result.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual List<TProjection> GroupBy<TDocument, TGroupKey, TProjection>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, TGroupKey>> groupingCriteria,
|
||||||
|
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TProjection : class, new()
|
||||||
|
{
|
||||||
|
return MongoDbReader.GroupBy<TDocument, TGroupKey, TProjection, TKey>(filter, groupingCriteria, groupProjection, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="sortSelector">The property selector.</param>
|
||||||
|
/// <param name="ascending">Order of the sorting.</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 virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, object>> sortSelector,
|
||||||
|
bool ascending = true,
|
||||||
|
int skipNumber = 0,
|
||||||
|
int takeNumber = 50,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.GetSortedPaginatedAsync<TDocument, TKey>(filter, sortSelector, ascending, skipNumber, takeNumber, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="sortDefinition">The sort definition.</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 virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
SortDefinition<TDocument> sortDefinition,
|
||||||
|
int skipNumber = 0,
|
||||||
|
int takeNumber = 50,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.GetSortedPaginatedAsync<TDocument, TKey>(filter, sortDefinition, skipNumber, takeNumber, partitionKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,469 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.DataAccess.Update;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
public interface IBaseMongoRepository_Update<TKey> where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously Updates a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||||
|
bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <param name="documentToModify">The document you want to modify.</param>
|
||||||
|
/// <param name="update">The update definition for the document.</param>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <param name="documentToModify">The document you want to modify.</param>
|
||||||
|
/// <param name="update">The update definition for the document.</param>
|
||||||
|
bool UpdateOne<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <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="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>
|
||||||
|
bool UpdateOne<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <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="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>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <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="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>
|
||||||
|
bool UpdateOne<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <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="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>
|
||||||
|
bool UpdateOne<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <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="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>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <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="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>
|
||||||
|
Task<bool> UpdateOneAsync<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, updates the property field with the given value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing 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>
|
||||||
|
Task<long> UpdateManyAsync<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, updates the property field with the given value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing 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>
|
||||||
|
Task<long> UpdateManyAsync<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, applies the update you have defined in MongoDb.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
Task<long> UpdateManyAsync<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, applies the update you have defined in MongoDb.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
Task<long> UpdateManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, updates the property field with the given value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing 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>
|
||||||
|
long UpdateMany<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, updates the property field with the given value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing 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>
|
||||||
|
long UpdateMany<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, applies the update you have defined in MongoDb.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
long UpdateMany<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, applies the update you have defined in MongoDb.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
long UpdateMany<TDocument>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract partial class BaseMongoRepository<TKey> : IBaseMongoRepository_Update<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
private MongoDbUpdater _mongoDbUpdater;
|
||||||
|
protected virtual MongoDbUpdater MongoDbUpdater
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_mongoDbUpdater != null) { return _mongoDbUpdater; }
|
||||||
|
|
||||||
|
lock (_initLock)
|
||||||
|
{
|
||||||
|
if (_mongoDbUpdater == null)
|
||||||
|
{
|
||||||
|
_mongoDbUpdater = new MongoDbUpdater(MongoDbContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return _mongoDbUpdater;
|
||||||
|
}
|
||||||
|
set { _mongoDbUpdater = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously Updates a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||||
|
public virtual async Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey>(modifiedDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates a document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||||
|
public virtual bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey>(modifiedDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <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>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey>(documentToModify, update);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <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>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey>(documentToModify, update);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="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, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(documentToModify, field, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="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, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(documentToModify, field, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="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, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="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, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="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, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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="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, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, updates the property field with the given value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing 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<long> UpdateManyAsync<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateManyAsync<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, updates the property field with the given value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing 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<long> UpdateManyAsync<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateManyAsync<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, applies the update you have defined in MongoDb.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public virtual async Task<long> UpdateManyAsync<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateManyAsync<TDocument, TKey>(filter, updateDefinition, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, applies the update you have defined in MongoDb.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public virtual async Task<long> UpdateManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbUpdater.UpdateManyAsync<TDocument, TKey>(filter, updateDefinition, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, updates the property field with the given value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing 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 long UpdateMany<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateMany<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, updates the property field with the given value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing 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 long UpdateMany<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateMany<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, applies the update you have defined in MongoDb.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public virtual long UpdateMany<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateMany<TDocument, TKey>(filter, updateDefinition, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For the entities selected by the filter, applies the update you have defined in MongoDb.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="updateDefinition">The update definition to apply.</param>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
public virtual long UpdateMany<TDocument>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbUpdater.UpdateMany<TDocument, TKey>(filter, updateDefinition, partitionKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using MongoDB.Bson.Serialization.Attributes;
|
using System;
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace MongoDbGenericRepository.Models
|
namespace MongoDbGenericRepository.Models
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -12,36 +12,18 @@ namespace MongoDbGenericRepository
|
|||||||
public class MongoDbContext : IMongoDbContext
|
public class MongoDbContext : IMongoDbContext
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The IMongoClient from the official MongoDb driver
|
/// The IMongoClient from the official MongoDB driver
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IMongoClient Client { get; }
|
public IMongoClient Client { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The IMongoDatabase from the official Mongodb driver
|
/// The IMongoDatabase from the official MongoDB driver
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IMongoDatabase Database { get; }
|
public IMongoDatabase Database { get; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sets the Guid representation of the MongoDb Driver.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
|
|
||||||
public virtual void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
|
|
||||||
{
|
|
||||||
MongoDefaults.GuidRepresentation = guidRepresentation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initialize the Guid representation of the MongoDb Driver.
|
/// The constructor of the MongoDbContext, it needs an object implementing <see cref="IMongoDatabase"/>.
|
||||||
/// Override this method to change the default GuidRepresentation.
|
|
||||||
/// </summary>
|
|
||||||
protected virtual void InitializeGuidRepresentation()
|
|
||||||
{
|
|
||||||
// by default, avoid lefacy UUID representation: use Binary 0x04 subtype.
|
|
||||||
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The constructor of the MongoDbContext, it needs a an object implementing <see cref="IMongoDatabase"/>.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="mongoDatabase">An object implementing IMongoDatabase</param>
|
/// <param name="mongoDatabase">An object implementing IMongoDatabase</param>
|
||||||
public MongoDbContext(IMongoDatabase mongoDatabase)
|
public MongoDbContext(IMongoDatabase mongoDatabase)
|
||||||
@@ -65,15 +47,24 @@ namespace MongoDbGenericRepository
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Extracts the CollectionName attribute from the entity type, if any.
|
/// Initialise an instance of a <see cref="IMongoDbContext"/> using a connection string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
/// <param name="connectionString"></param>
|
||||||
/// <returns>The name of the collection in which the TDocument is stored.</returns>
|
public MongoDbContext(string connectionString)
|
||||||
private string GetAttributeCollectionName<TDocument>()
|
: this(connectionString, new MongoUrl(connectionString).DatabaseName)
|
||||||
{
|
{
|
||||||
return (typeof(TDocument).GetTypeInfo()
|
}
|
||||||
.GetCustomAttributes(typeof(CollectionNameAttribute))
|
|
||||||
.FirstOrDefault() as CollectionNameAttribute)?.Name;
|
/// <summary>
|
||||||
|
/// The constructor of the MongoDbContext, it needs a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">The MongoClient.</param>
|
||||||
|
/// <param name="databaseName">The name of your database.</param>
|
||||||
|
public MongoDbContext(MongoClient client, string databaseName)
|
||||||
|
{
|
||||||
|
InitializeGuidRepresentation();
|
||||||
|
Client = client;
|
||||||
|
Database = client.GetDatabase(databaseName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -81,7 +72,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
/// <param name="partitionKey">The optional value of the partition key.</param>
|
/// <param name="partitionKey">The optional value of the partition key.</param>
|
||||||
public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null)
|
public virtual IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null)
|
||||||
{
|
{
|
||||||
return Database.GetCollection<TDocument>(GetCollectionName<TDocument>(partitionKey));
|
return Database.GetCollection<TDocument>(GetCollectionName<TDocument>(partitionKey));
|
||||||
}
|
}
|
||||||
@@ -90,18 +81,49 @@ namespace MongoDbGenericRepository
|
|||||||
/// Drops a collection, use very carefully.
|
/// Drops a collection, use very carefully.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
public void DropCollection<TDocument>(string partitionKey = null)
|
public virtual void DropCollection<TDocument>(string partitionKey = null)
|
||||||
{
|
{
|
||||||
Database.DropCollection(GetCollectionName<TDocument>(partitionKey));
|
Database.DropCollection(GetCollectionName<TDocument>(partitionKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Given the docmuent type and the partition key, returns the name of the collection it belongs to.
|
/// Sets the Guid representation of the MongoDB Driver.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
|
||||||
|
public virtual void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
|
||||||
|
{
|
||||||
|
MongoDefaults.GuidRepresentation = guidRepresentation;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extracts the CollectionName attribute from the entity type, if any.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
/// <param name="partitionKey">The value of the partition key.</param>
|
/// <returns>The name of the collection in which the TDocument is stored.</returns>
|
||||||
|
protected virtual string GetAttributeCollectionName<TDocument>()
|
||||||
|
{
|
||||||
|
return (typeof(TDocument).GetTypeInfo()
|
||||||
|
.GetCustomAttributes(typeof(CollectionNameAttribute))
|
||||||
|
.FirstOrDefault() as CollectionNameAttribute)?.Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize the Guid representation of the MongoDB Driver.
|
||||||
|
/// Override this method to change the default GuidRepresentation.
|
||||||
|
/// </summary>
|
||||||
|
protected virtual void InitializeGuidRepresentation()
|
||||||
|
{
|
||||||
|
// by default, avoid legacy UUID representation: use Binary 0x04 subtype.
|
||||||
|
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Given the document type and the partition key, returns the name of the collection it belongs to.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
/// <returns>The name of the collection.</returns>
|
/// <returns>The name of the collection.</returns>
|
||||||
private string GetCollectionName<TDocument>(string partitionKey)
|
protected virtual string GetCollectionName<TDocument>(string partitionKey)
|
||||||
{
|
{
|
||||||
var collectionName = GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>();
|
var collectionName = GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>();
|
||||||
if (string.IsNullOrEmpty(partitionKey))
|
if (string.IsNullOrEmpty(partitionKey))
|
||||||
@@ -116,9 +138,9 @@ namespace MongoDbGenericRepository
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
/// <returns>The pluralized document name.</returns>
|
/// <returns>The pluralized document name.</returns>
|
||||||
private string Pluralize<TDocument>()
|
protected virtual string Pluralize<TDocument>()
|
||||||
{
|
{
|
||||||
return (typeof(TDocument).Name.Pluralize()).Camelize();
|
return (typeof(TDocument).Name.Pluralize()).Camelize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,38 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net45;netstandard1.5;netstandard2.0</TargetFrameworks>
|
<TargetFrameworks>net452;netstandard2.0;netstandard1.5;</TargetFrameworks>
|
||||||
<PackageId>MongoDbGenericRepository</PackageId>
|
<PackageId>MongoDbGenericRepository</PackageId>
|
||||||
<PackageVersion>1.2.0</PackageVersion>
|
<PackageVersion>1.4.4</PackageVersion>
|
||||||
<Authors>Alexandre Spieser</Authors>
|
<Authors>Alexandre Spieser</Authors>
|
||||||
|
<PackageTitle>MongoDb Generic Repository</PackageTitle>
|
||||||
<Description>A generic repository implementation using the MongoDB C# Sharp 2.0 driver.</Description>
|
<Description>A generic repository implementation using the MongoDB C# Sharp 2.0 driver.</Description>
|
||||||
|
<PackageLicenseUrl>http://www.opensource.org/licenses/mit-license.php</PackageLicenseUrl>
|
||||||
|
<PackageProjectUrl>http://www.opensource.org/licenses/mit-license.php</PackageProjectUrl>
|
||||||
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
|
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
|
||||||
<PackageReleaseNotes>.NET Core supported.</PackageReleaseNotes>
|
<PackageReleaseNotes>Release notes are at https://github.com/alexandre-spieser/mongodb-generic-repository/releases </PackageReleaseNotes>
|
||||||
<Copyright>Copyright 2017 (c) Alexandre Spieser. All rights reserved.</Copyright>
|
<Copyright>Copyright 2020 (c) Alexandre Spieser. All rights reserved.</Copyright>
|
||||||
<PackageTags>MongoDb Repository NoSql Generic</PackageTags>
|
<PackageTags>MongoDb Repository Generic NoSql</PackageTags>
|
||||||
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
|
<Version>1.4.4</Version>
|
||||||
|
<RepositoryUrl>https://github.com/alexandre-spieser/mongodb-generic-repository</RepositoryUrl>
|
||||||
|
<RepositoryType>Git</RepositoryType>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net45|AnyCPU'">
|
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net45|AnyCPU'">
|
||||||
<DocumentationFile>bin\Release\net45\MongoDbGenericRepository.xml</DocumentationFile>
|
<DocumentationFile>bin\Release\net45\MongoDbGenericRepository.xml</DocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net452|AnyCPU'">
|
||||||
|
<DocumentationFile></DocumentationFile>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net452|AnyCPU'">
|
||||||
|
<DocumentationFile>D:\development\mongodb-generic-repository\MongoDbGenericRepository\MongoDbGenericRepository.xml</DocumentationFile>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.7.0" />
|
<PackageReference Include="MongoDB.Driver" Version="2.9.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<package >
|
<package >
|
||||||
<metadata>
|
<metadata>
|
||||||
<id>MongoDbGenericRepository</id>
|
<id>MongoDbGenericRepository</id>
|
||||||
<version>1.3.8</version>
|
<version>1.4.4</version>
|
||||||
<title>MongoDb Generic Repository</title>
|
<title>MongoDb Generic Repository</title>
|
||||||
<authors>Alexandre Spieser</authors>
|
<authors>Alexandre Spieser</authors>
|
||||||
<owners>Alexandre Spieser</owners>
|
<owners>Alexandre Spieser</owners>
|
||||||
@@ -11,10 +11,18 @@
|
|||||||
<requireLicenseAcceptance>false</requireLicenseAcceptance>
|
<requireLicenseAcceptance>false</requireLicenseAcceptance>
|
||||||
<description>A generic repository implementation using the MongoDB C# Sharp 2.0 driver.</description>
|
<description>A generic repository implementation using the MongoDB C# Sharp 2.0 driver.</description>
|
||||||
<releaseNotes>Release notes are at https://github.com/alexandre-spieser/mongodb-generic-repository/releases</releaseNotes>
|
<releaseNotes>Release notes are at https://github.com/alexandre-spieser/mongodb-generic-repository/releases</releaseNotes>
|
||||||
<copyright>Copyright 2018 (c) Alexandre Spieser. All rights reserved.</copyright>
|
<copyright>Copyright 2020 (c) Alexandre Spieser. All rights reserved.</copyright>
|
||||||
<tags>MongoDb Repository Generic NoSql</tags>
|
<tags>MongoDb Repository Generic NoSql</tags>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency id="MongoDB.Driver" version="2.7.0" />
|
<group targetFramework=".NETFramework4.5.2">
|
||||||
|
<dependency id="MongoDB.Driver" version="2.9.3" exclude="Build,Analyzers" />
|
||||||
|
</group>
|
||||||
|
<group targetFramework=".NETStandard1.5">
|
||||||
|
<dependency id="MongoDB.Driver" version="2.9.3" exclude="Build,Analyzers" />
|
||||||
|
</group>
|
||||||
|
<group targetFramework=".NETStandard2.0">
|
||||||
|
<dependency id="MongoDB.Driver" version="2.9.3" exclude="Build,Analyzers"/>
|
||||||
|
</group>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</metadata>
|
</metadata>
|
||||||
<files>
|
<files>
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,243 +1,45 @@
|
|||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.DataAccess.Read;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using MongoDbGenericRepository.Models;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace MongoDbGenericRepository
|
namespace MongoDbGenericRepository
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The ReadOnlyMongoRepository implements the readonly functionality of the IReadOnlyMongoRepository.
|
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
|
||||||
|
/// Its constructor must be given a connection string and a database name.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ReadOnlyMongoRepository : IReadOnlyMongoRepository
|
public abstract class ReadOnlyMongoRepository : ReadOnlyMongoRepository<Guid>, IReadOnlyMongoRepository
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// The connection string.
|
|
||||||
/// </summary>
|
|
||||||
public string ConnectionString { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The database name.
|
|
||||||
/// </summary>
|
|
||||||
public string DatabaseName { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The MongoDbContext
|
|
||||||
/// </summary>
|
|
||||||
protected IMongoDbContext MongoDbContext = null;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The constructor taking a connection string and a database name.
|
/// The constructor taking a connection string and a database name.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="connectionString">The connection string of the MongoDb server.</param>
|
/// <param name="connectionString">The connection string of the MongoDb server.</param>
|
||||||
/// <param name="databaseName">The name of the database against which you want to perform operations.</param>
|
/// <param name="databaseName">The name of the database against which you want to perform operations.</param>
|
||||||
protected ReadOnlyMongoRepository(string connectionString, string databaseName)
|
protected ReadOnlyMongoRepository(string connectionString, string databaseName = null) : base(connectionString, databaseName)
|
||||||
{
|
{
|
||||||
MongoDbContext = new MongoDbContext(connectionString, databaseName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The contructor taking a <see cref="IMongoDbContext"/>.
|
/// The contructor taking a <see cref="IMongoDbContext"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
||||||
protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext)
|
protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||||
{
|
{
|
||||||
MongoDbContext = mongoDbContext;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The contructor taking a <see cref="IMongoDatabase"/>.
|
/// The contructor taking a <see cref="IMongoDatabase"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
||||||
protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase)
|
protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
|
||||||
{
|
{
|
||||||
MongoDbContext = new MongoDbContext(mongoDatabase);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Read
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns one document given its id.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="id">The Id of the document you want to get.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public async Task<TDocument> GetByIdAsync<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
|
||||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns one document given its id.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="id">The Id of the document you want to get.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public TDocument GetById<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
|
||||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns one document given an expression filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public async Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns one document given an expression filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a collection cursor.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if any of the document of the collection matches the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public async Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
var count = await HandlePartitioned<TDocument>(partitionKey).CountDocumentsAsync(filter);
|
|
||||||
return (count > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if any of the document of the collection matches the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
var count = HandlePartitioned<TDocument>(partitionKey).CountDocuments(filter);
|
|
||||||
return (count > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns a list of the documents matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public async Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a list of the documents matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously counts how many documents match the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey</param>
|
|
||||||
public async Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await HandlePartitioned<TDocument>(partitionKey).CountDocumentsAsync(filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Counts how many documents match the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey</param>
|
|
||||||
public long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).CountDocuments();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
public async Task<TDocument> GetByMaxAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await GetByMaxAsync<TDocument, Guid>(filter, maxValueSelector, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public TDocument GetByMax<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return GetByMax<TDocument, Guid>(filter, maxValueSelector, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
public async Task<TDocument> GetByMinAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await GetByMinAsync<TDocument, Guid>(filter, minValueSelector, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
public TDocument GetByMin<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return GetByMin<TDocument, Guid>(filter, orderByAscending, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Read TKey
|
#region Read TKey
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -247,12 +49,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||||
/// <param name="id">The Id of the document you want to get.</param>
|
/// <param name="id">The Id of the document you want to get.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public async Task<TDocument> GetByIdAsync<TDocument, TKey>(TKey id, string partitionKey = null)
|
public async virtual Task<TDocument> GetByIdAsync<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
return await MongoDbReader.GetByIdAsync<TDocument, TKey>(id, partitionKey);
|
||||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -262,12 +63,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||||
/// <param name="id">The Id of the document you want to get.</param>
|
/// <param name="id">The Id of the document you want to get.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public TDocument GetById<TDocument, TKey>(TKey id, string partitionKey = null)
|
public virtual TDocument GetById<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
return MongoDbReader.GetById<TDocument, TKey>(id, partitionKey);
|
||||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefault();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -277,11 +77,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public async Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
public async virtual Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
return await MongoDbReader.GetOneAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -291,11 +91,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
public virtual TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefault();
|
return MongoDbReader.GetOne<TDocument, TKey>(filter, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -305,11 +105,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
public virtual IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter);
|
return MongoDbReader.GetCursor<TDocument, TKey>(filter, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -319,12 +119,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public async Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
public async virtual Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
var count = await HandlePartitioned<TDocument, TKey>(partitionKey).CountDocumentsAsync(filter);
|
return await MongoDbReader.AnyAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
return (count > 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -334,12 +133,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
public virtual bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
var count = HandlePartitioned<TDocument, TKey>(partitionKey).CountDocuments(filter);
|
return MongoDbReader.Any<TDocument, TKey>(filter, partitionKey);
|
||||||
return (count > 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -349,11 +147,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public async Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
public async virtual Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).ToListAsync();
|
return await MongoDbReader.GetAllAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -363,11 +161,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
public virtual List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).ToList();
|
return MongoDbReader.GetAll<TDocument, TKey>(filter, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -377,11 +175,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="partitionKey">An optional partitionKey</param>
|
/// <param name="partitionKey">An optional partitionKey</param>
|
||||||
public async Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
public async virtual Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).CountDocumentsAsync(filter);
|
return await MongoDbReader.CountAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -391,11 +189,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="partitionKey">An optional partitionKey</param>
|
/// <param name="partitionKey">An optional partitionKey</param>
|
||||||
public long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
public virtual long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).CountDocuments();
|
return MongoDbReader.Count<TDocument, TKey>(filter, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -406,14 +204,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
public async Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
public async virtual Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return await GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
return await MongoDbReader.GetByMaxAsync<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||||
.SortByDescending(maxValueSelector)
|
|
||||||
.Limit(1)
|
|
||||||
.FirstOrDefaultAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -424,14 +219,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
public TDocument GetByMax<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
public virtual TDocument GetByMax<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
return MongoDbReader.GetByMax<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||||
.SortByDescending(maxValueSelector)
|
|
||||||
.Limit(1)
|
|
||||||
.FirstOrDefault();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -442,14 +234,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
public async Task<TDocument> GetByMinAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
public async virtual Task<TDocument> GetByMinAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return await GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
return await MongoDbReader.GetByMinAsync<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||||
.SortBy(minValueSelector)
|
|
||||||
.Limit(1)
|
|
||||||
.FirstOrDefaultAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -460,64 +249,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
public TDocument GetByMin<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
public virtual TDocument GetByMin<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
return MongoDbReader.GetByMin<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||||
.SortBy(minValueSelector)
|
|
||||||
.Limit(1)
|
|
||||||
.FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
private IFindFluent<TDocument, TDocument> GetMinMongoQuery<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
{
|
|
||||||
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
|
||||||
.SortBy(ConvertExpression(minValueSelector))
|
|
||||||
.Limit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
private IFindFluent<TDocument, TDocument> GetMaxMongoQuery<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
{
|
|
||||||
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
|
||||||
.SortByDescending(ConvertExpression(maxValueSelector))
|
|
||||||
.Limit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
public async Task<TValue> GetMaxValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await GetMaxValueAsync<TDocument, Guid, TValue>(filter, maxValueSelector, partitionKey);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -525,30 +261,15 @@ namespace MongoDbGenericRepository
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
/// <param name="maxValueSelector">A property selector to select the max value.</param>
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
public async Task<TValue> GetMaxValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
public async virtual Task<TValue> GetMaxValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return await GetMaxMongoQuery<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey)
|
return await MongoDbReader.GetMaxValueAsync<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||||
.Project(maxValueSelector)
|
|
||||||
.FirstOrDefaultAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
public TValue GetMaxValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return GetMaxValue<TDocument, Guid, TValue>(filter, maxValueSelector, partitionKey);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -560,28 +281,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
public TValue GetMaxValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
public virtual TValue GetMaxValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
|
return MongoDbReader.GetMaxValue<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||||
return GetMaxMongoQuery<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey)
|
|
||||||
.Project(maxValueSelector)
|
|
||||||
.FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public async Task<TValue> GetMinValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await GetMinValueAsync<TDocument, Guid, TValue>(filter, minValueSelector, partitionKey);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -593,25 +297,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public async Task<TValue> GetMinValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
public virtual async Task<TValue> GetMinValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return await GetMinMongoQuery<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefaultAsync();
|
return await MongoDbReader.GetMinValueAsync<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public TValue GetMinValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return GetMinValue<TDocument, Guid, TValue>(filter, minValueSelector, partitionKey);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -623,43 +313,276 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public TValue GetMinValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
public virtual TValue GetMinValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return GetMinMongoQuery<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefault();
|
return MongoDbReader.GetMinValue<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Utility Methods
|
#region Sum TKey
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a collections for the type TDocument with the matching partition key (if any).
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual async Task<int> SumByAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, int>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.SumByAsync<TDocument, TKey>(filter, selector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual int SumBy<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, int>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.SumBy<TDocument, TKey>(filter, selector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual async Task<decimal> SumByAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, decimal>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await MongoDbReader.SumByAsync<TDocument, TKey>(filter, selector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sums the values of a selected field for a given filtered collection of documents.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual decimal SumBy<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, decimal>> selector,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.SumBy<TDocument, TKey>(filter, selector, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Sum TKey
|
||||||
|
|
||||||
|
#region Project TKey
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a projected document matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
/// <returns>An <see cref="IMongoCollection{TDocument}"/></returns>
|
public virtual async Task<TProjection> ProjectOneAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
protected IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null) where TDocument : IDocument
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class
|
||||||
{
|
{
|
||||||
return MongoDbContext.GetCollection<TDocument>(partitionKey);
|
return await MongoDbReader.ProjectOneAsync<TDocument, TProjection, TKey>(filter, projection, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a collections for the type TDocument
|
/// Returns a projected document matching the filter condition.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
/// <param name="document">The document.</param>
|
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||||
/// <returns></returns>
|
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
protected IMongoCollection<TDocument> HandlePartitioned<TDocument>(TDocument document) where TDocument : IDocument
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual TProjection ProjectOne<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class
|
||||||
{
|
{
|
||||||
if (document is IPartitionedDocument)
|
return MongoDbReader.ProjectOne<TDocument, TProjection, TKey>(filter, projection, partitionKey);
|
||||||
{
|
|
||||||
return GetCollection<TDocument>(((IPartitionedDocument)document).PartitionKey);
|
|
||||||
}
|
|
||||||
return GetCollection<TDocument>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual async Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return await MongoDbReader.ProjectManyAsync<TDocument, TProjection, TKey>(filter, projection, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </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="TProjection">The type representing the model you want to project to.</typeparam>
|
||||||
|
/// <param name="filter">The document filter.</param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual List<TProjection> ProjectMany<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return MongoDbReader.ProjectMany<TDocument, TProjection, TKey>(filter, projection, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Project TKey
|
||||||
|
|
||||||
|
#region Group By TKey
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Groups filtered a collection of documents given a grouping criteria,
|
||||||
|
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
|
||||||
|
/// </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="TGroupKey">The type of the grouping criteria.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
||||||
|
/// <param name="groupingCriteria">The grouping criteria.</param>
|
||||||
|
/// <param name="groupProjection">The projected group result.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual List<TProjection> GroupBy<TDocument, TGroupKey, TProjection, TKey>(
|
||||||
|
Expression<Func<TDocument, TGroupKey>> groupingCriteria,
|
||||||
|
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class, new()
|
||||||
|
{
|
||||||
|
return MongoDbReader.GroupBy<TDocument, TGroupKey, TProjection, TKey>(groupingCriteria, groupProjection, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Groups filtered a collection of documents given a grouping criteria,
|
||||||
|
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
|
||||||
|
/// </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="TGroupKey">The type of the grouping criteria.</typeparam>
|
||||||
|
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="groupingCriteria">The grouping criteria.</param>
|
||||||
|
/// <param name="groupProjection">The projected group result.</param>
|
||||||
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
|
public virtual List<TProjection> GroupBy<TDocument, TGroupKey, TProjection, TKey>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, TGroupKey>> groupingCriteria,
|
||||||
|
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
where TProjection : class, new()
|
||||||
|
{
|
||||||
|
return MongoDbReader.GroupBy<TDocument, TGroupKey, TProjection, TKey>(filter, groupingCriteria, groupProjection, partitionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Group By TKey
|
||||||
|
|
||||||
|
|
||||||
|
#region Pagination
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="sortSelector">The property selector.</param>
|
||||||
|
/// <param name="ascending">Order of the sorting.</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 virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument, TKey>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
Expression<Func<TDocument, object>> sortSelector,
|
||||||
|
bool ascending = true,
|
||||||
|
int skipNumber = 0,
|
||||||
|
int takeNumber = 50,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
var sorting = ascending
|
||||||
|
? Builders<TDocument>.Sort.Ascending(sortSelector)
|
||||||
|
: Builders<TDocument>.Sort.Descending(sortSelector);
|
||||||
|
|
||||||
|
return await HandlePartitioned<TDocument, TKey>(partitionKey)
|
||||||
|
.Find(filter)
|
||||||
|
.Sort(sorting)
|
||||||
|
.Skip(skipNumber)
|
||||||
|
.Limit(takeNumber)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
/// </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="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="sortDefinition">The sort definition.</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 virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument, TKey>(
|
||||||
|
Expression<Func<TDocument, bool>> filter,
|
||||||
|
SortDefinition<TDocument> sortDefinition,
|
||||||
|
int skipNumber = 0,
|
||||||
|
int takeNumber = 50,
|
||||||
|
string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return await HandlePartitioned<TDocument, TKey>(partitionKey)
|
||||||
|
.Find(filter)
|
||||||
|
.Sort(sortDefinition)
|
||||||
|
.Skip(skipNumber)
|
||||||
|
.Limit(takeNumber)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Pagination
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a collections for a potentially partitioned document type.
|
/// Gets a collections for a potentially partitioned document type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -667,44 +590,11 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
/// <param name="document">The document.</param>
|
/// <param name="document">The document.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected IMongoCollection<TDocument> HandlePartitioned<TDocument, TKey>(TDocument document)
|
public virtual IMongoCollection<TDocument> HandlePartitioned<TDocument, TKey>(TDocument document)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
if (document is IPartitionedDocument)
|
return MongoDbReader.HandlePartitioned<TDocument, TKey>(document);
|
||||||
{
|
|
||||||
return GetCollection<TDocument, TKey>(((IPartitionedDocument)document).PartitionKey);
|
|
||||||
}
|
|
||||||
return GetCollection<TDocument, TKey>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a collections for a potentially partitioned document type.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="partitionKey">The collection partition key.</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
protected IMongoCollection<TDocument> HandlePartitioned<TDocument>(string partitionKey) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
if (!string.IsNullOrEmpty(partitionKey))
|
|
||||||
{
|
|
||||||
return GetCollection<TDocument>(partitionKey);
|
|
||||||
}
|
|
||||||
return GetCollection<TDocument>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a collections for the type TDocument with a partition key.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
|
||||||
/// <param name="partitionKey">The collection partition key.</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
protected IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
{
|
|
||||||
return MongoDbContext.GetCollection<TDocument>(partitionKey);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -714,7 +604,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
/// <param name="partitionKey">The collection partition key.</param>
|
/// <param name="partitionKey">The collection partition key.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected IMongoCollection<TDocument> HandlePartitioned<TDocument, TKey>(string partitionKey)
|
public virtual IMongoCollection<TDocument> HandlePartitioned<TDocument, TKey>(string partitionKey)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
@@ -726,20 +616,29 @@ namespace MongoDbGenericRepository
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts a LINQ expression of TDocument, TValue to a LINQ expression of TDocument, object
|
/// Gets a collections for a potentially partitioned document type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
/// <typeparam name="TValue">The type of the value.</typeparam>
|
/// <param name="document">The document.</param>
|
||||||
/// <param name="expression">The expression to convert</param>
|
/// <returns></returns>
|
||||||
protected static Expression<Func<TDocument, object>> ConvertExpression<TDocument, TValue>(Expression<Func<TDocument, TValue>> expression)
|
public virtual IMongoCollection<TDocument> HandlePartitioned<TDocument>(TDocument document)
|
||||||
|
where TDocument : IDocument<Guid>
|
||||||
{
|
{
|
||||||
var param = expression.Parameters[0];
|
return MongoDbReader.HandlePartitioned<TDocument, Guid>(document);
|
||||||
Expression body = expression.Body;
|
|
||||||
var convert = Expression.Convert(body, typeof(object));
|
|
||||||
return Expression.Lambda<Func<TDocument, object>>(convert, param);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
/// <summary>
|
||||||
|
/// Gets a collections for the type TDocument with a partition key.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <param name="partitionKey">The collection partition key.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
return MongoDbReader.GetCollection<TDocument, TKey>(partitionKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,26 @@
|
|||||||
# MongoDbGenericRepository
|
# MongoDbGenericRepository
|
||||||
|
|
||||||
An example of generic repository implementation using the MongoDB C# Sharp 2.0 driver (async)
|
An example of generic repository implementation using the MongoDB C# Sharp 2.0 driver (async)
|
||||||
|
|
||||||
Now available as a nuget package:
|
Now available as a nuget package:
|
||||||
https://www.nuget.org/packages/MongoDbGenericRepository/
|
https://www.nuget.org/packages/MongoDbGenericRepository/
|
||||||
|
|
||||||
Covered by 200+ integration tests and counting.
|
Covered by 400+ integration tests and counting.
|
||||||
|
|
||||||
The MongoDbGenericRepository is also used in [AspNetCore.Identity.MongoDbCore](https://github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore).
|
The MongoDbGenericRepository is also used in [AspNetCore.Identity.MongoDbCore](https://github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore).
|
||||||
|
|
||||||
|
# Support This Project
|
||||||
|
|
||||||
|
If you have found this project helpful, either as a library that you use or as a learning tool, please consider buying the team a coffee: <a href="https://www.buymeacoffee.com/zeitquest" target="_blank"><img height="40px" src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" style="max-height: 51px;width: 150px !important;" ></a>
|
||||||
|
|
||||||
|
# Worth Knowing
|
||||||
|
|
||||||
|
This package sets the `MongoDefaults.GuidRepresentation` to `MongoDB.Bson.GuidRepresentation.Standard` by default, instead of the default driver setting of `MongoDB.Bson.GuidRepresentation.CSharpLegacy`. This can cause issues if you have been using the driver on an existing application previously or if you are using CosmosDB.
|
||||||
|
|
||||||
|
You can override this behaviour to enforce legacy behaviour in your app Startup routine like so :
|
||||||
|
|
||||||
|
`MongoDbContext.SetGuidRepresentation(MongoDB.Bson.GuidRepresentation.CSharpLegacy)`. More info [here](https://github.com/alexandre-spieser/mongodb-generic-repository/issues/7).
|
||||||
|
|
||||||
# Usage examples
|
# Usage examples
|
||||||
|
|
||||||
This repository is meant to be inherited from.
|
This repository is meant to be inherited from.
|
||||||
@@ -40,6 +53,18 @@ Here is an example of repository usage, where the TestRepository is implementing
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
If all your documents have the same type of `Id`, you can use the more specific `BaseMongoRepository<TKey>` where `TKey` is the type of the `Id` of your documents.
|
||||||
|
```csharp
|
||||||
|
public class TestTKeyRepository<TKey> : BaseMongoRepository<TKey>, ITestRepository<TKey> where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
const string connectionString = "mongodb://localhost:27017/MongoDbTests";
|
||||||
|
private static readonly ITestRepository<TKey> _instance = new TestTKeyRepository<TKey>(connectionString);
|
||||||
|
/// <inheritdoc />
|
||||||
|
private TestTKeyRepository(string connectionString) : base(connectionString)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Instantiation
|
## Instantiation
|
||||||
|
|
||||||
@@ -47,6 +72,7 @@ The repository can be instantiated like so:
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
ITestRepository testRepository = new TestRepository(connectionString, "MongoDbTests");
|
ITestRepository testRepository = new TestRepository(connectionString, "MongoDbTests");
|
||||||
|
ITestRepository<TKey> testTKeyRepository = new TestTKeyRepository<TKey>(connectionString);
|
||||||
```
|
```
|
||||||
|
|
||||||
If you prefer to reuse the same MongoDb database across your application, you can use the `MongoDatabase` from the MongoDb driver implementing the `IMongoDatabase` interface:
|
If you prefer to reuse the same MongoDb database across your application, you can use the `MongoDatabase` from the MongoDb driver implementing the `IMongoDatabase` interface:
|
||||||
@@ -144,7 +170,40 @@ It is now possible to change the collection name by using the `CollectionName` a
|
|||||||
```
|
```
|
||||||
Documents of this type will be inserted into a collection named "MyCollectionName".
|
Documents of this type will be inserted into a collection named "MyCollectionName".
|
||||||
|
|
||||||
|
## Index Management
|
||||||
|
From version 1.3.8 the `MongoDbGenericRepository` implements the `IBaseMongoRepository_Index` and `IBaseMongoRepository_Index<TKey>` interfaces.
|
||||||
|
This exposes the functionality to programmatically manage indexes against your collections in a generic fashion.
|
||||||
|
|
||||||
|
The following methods are exposed and fully integration tested:
|
||||||
|
+ CreateAscendingIndexAsync
|
||||||
|
+ CreateDescendingIndexAsync
|
||||||
|
+ CreateCombinedTextIndexAsync
|
||||||
|
+ CreateHashedIndexAsync
|
||||||
|
+ CreateTextIndexAsync
|
||||||
|
+ DropIndexAsync
|
||||||
|
+ GetIndexesNamesAsync
|
||||||
|
|
||||||
|
Usage examples:
|
||||||
|
```csharp
|
||||||
|
string expectedIndexName = $"myCustomIndexName";
|
||||||
|
var option = new IndexCreationOptions
|
||||||
|
{
|
||||||
|
Name = expectedIndexName
|
||||||
|
};
|
||||||
|
// Act
|
||||||
|
// create a text index against the Version property of the document.
|
||||||
|
var result = await SUT.CreateTextIndexAsync<T, TKey>(x => x.Version, option, PartitionKey);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var listOfIndexNames = await SUT.GetIndexesNamesAsync<T, TKey>(PartitionKey);
|
||||||
|
Assert.Contains(expectedIndexName, listOfIndexNames);
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
await SUT.DropIndexAsync<T, TKey>(expectedIndexName, PartitionKey);
|
||||||
|
```
|
||||||
|
|
||||||
Please refer to the IntegrationTests (NET45) and CoreIntegrationTests (netstandard2.0) projects for more usage examples.
|
Please refer to the IntegrationTests (NET45) and CoreIntegrationTests (netstandard2.0) projects for more usage examples.
|
||||||
|
The `CoreIntegrationTests.Infrastructure.MongoDbTKeyDocumentTestBase<T, TKey>` test class is a good start.
|
||||||
|
|
||||||
## Author
|
## Author
|
||||||
**Alexandre Spieser**
|
**Alexandre Spieser**
|
||||||
@@ -154,7 +213,7 @@ mongodb-generic-repository is under MIT license - http://www.opensource.org/lice
|
|||||||
|
|
||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2016-2018 Alexandre Spieser
|
Copyright (c) 2016-2019 Alexandre Spieser
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@@ -189,4 +248,4 @@ Copyright (c) 2012-2014 Mehdi Khalili (http://omar.io)
|
|||||||
==============================================================================
|
==============================================================================
|
||||||
|
|
||||||
## Copyright
|
## Copyright
|
||||||
Copyright © 2018
|
Copyright © 2019
|
||||||
|
|||||||
Reference in New Issue
Block a user