Merge pull request #42 from khanhna/feature/read_process_added

Add read operation support for filter definition
This commit is contained in:
Alexandre SPIESER
2021-12-06 19:14:55 +00:00
committed by GitHub
5 changed files with 347 additions and 0 deletions
@@ -49,6 +49,30 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously returns one document given filter 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="condition">A mongodb filter definition.</param>
/// <param name="findOption">A mongodb filter option.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">An optional cancellation Token.</param>
Task<TDocument> GetOneAsync<TDocument, TKey>(FilterDefinition<TDocument> condition, FindOptions findOption = null,
string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>;
/// <summary>
/// Returns one document given filter 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="condition">A mongodb filter definition.</param>
/// <param name="findOption">A mongodb filter option.</param>
/// <param name="partitionKey">An optional partition key.</param>
TDocument GetOne<TDocument, TKey>(FilterDefinition<TDocument> condition, FindOptions findOption = null, string partitionKey = null)
where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>;
/// <summary> /// <summary>
/// Asynchronously returns one document given an expression filter. /// Asynchronously returns one document given an expression filter.
/// </summary> /// </summary>
@@ -83,6 +107,29 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<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="condition">A mongodb filter definition.</param>
/// <param name="countOption">A mongodb counting option.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">An optional cancellation Token.</param>
Task<bool> AnyAsync<TDocument, TKey>(FilterDefinition<TDocument> condition, CountOptions countOption = null, string partitionKey = null,
CancellationToken cancellationToken = default)
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="condition">A mongodb filter definition.</param>
/// <param name="countOption">A mongodb counting option.</param>
/// <param name="partitionKey">An optional partition key.</param>
bool Any<TDocument, TKey>(FilterDefinition<TDocument> condition, CountOptions countOption = null, string partitionKey = null)
where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>;
/// <summary> /// <summary>
/// Returns true if any of the document of the collection matches the filter condition. /// Returns true if any of the document of the collection matches the filter condition.
@@ -107,6 +154,30 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<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="condition">A mongodb filter definition.</param>
/// <param name="findOption">A mongodb filter option.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">An optional cancellation Token.</param>
Task<List<TDocument>> GetAllAsync<TDocument, TKey>(FilterDefinition<TDocument> condition,
FindOptions findOption = null, string partitionKey = null, CancellationToken cancellationToken = default)
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="condition">A mongodb filter definition.</param>
/// <param name="findOption">A mongodb filter option.</param>
/// <param name="partitionKey">An optional partition key.</param>
List<TDocument> GetAll<TDocument, TKey>(FilterDefinition<TDocument> condition, FindOptions findOption = null, string partitionKey = null)
where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>;
/// <summary> /// <summary>
/// Asynchronously returns a list of the documents matching the filter condition. /// Asynchronously returns a list of the documents matching the filter condition.
/// </summary> /// </summary>
@@ -130,6 +201,31 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<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="condition">A mongodb filter definition.</param>
/// <param name="countOption">A mongodb counting option.</param>
/// <param name="partitionKey">An optional partitionKey</param>
/// <param name="cancellationToken">An optional cancellation Token.</param>
Task<long> CountAsync<TDocument, TKey>(FilterDefinition<TDocument> condition, CountOptions countOption = null,
string partitionKey = null, CancellationToken cancellationToken = default)
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="condition">A mongodb filter definition.</param>
/// <param name="countOption">A mongodb counting option.</param>
/// <param name="partitionKey">An optional partitionKey</param>
long Count<TDocument, TKey>(FilterDefinition<TDocument> condition, CountOptions countOption = null,
string partitionKey = null)
where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>;
/// <summary> /// <summary>
/// Asynchronously counts how many documents match the filter condition. /// Asynchronously counts how many documents match the filter condition.
/// </summary> /// </summary>
@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDB.Driver; using MongoDB.Driver;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
@@ -57,6 +57,39 @@ namespace MongoDbGenericRepository.DataAccess.Read
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefault(); return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefault();
} }
/// <summary>
/// Asynchronously returns one document given filter 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="condition">A mongodb filter definition.</param>
/// <param name="findOption">A mongodb filter option.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual Task<TDocument> GetOneAsync<TDocument, TKey>(FilterDefinition<TDocument> condition, FindOptions findOption = null,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(condition, findOption).FirstOrDefaultAsync(cancellationToken);
}
/// <summary>
/// Returns one document given filter 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="condition">A mongodb filter definition.</param>
/// <param name="findOption">A mongodb filter option.</param>
/// <param name="partitionKey">An optional partition key.</param>
public virtual TDocument GetOne<TDocument, TKey>(FilterDefinition<TDocument> condition, FindOptions findOption = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(condition, findOption).FirstOrDefault();
}
/// <summary> /// <summary>
/// Asynchronously returns one document given an expression filter. /// Asynchronously returns one document given an expression filter.
/// </summary> /// </summary>
@@ -100,6 +133,41 @@ namespace MongoDbGenericRepository.DataAccess.Read
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter); 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="condition">A mongodb filter definition.</param>
/// <param name="countOption">A mongodb counting option.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual async Task<bool> AnyAsync<TDocument, TKey>(FilterDefinition<TDocument> condition, CountOptions countOption = null, string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var count = await HandlePartitioned<TDocument, TKey>(partitionKey).CountDocumentsAsync(condition, countOption, cancellationToken);
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="condition">A mongodb filter definition.</param>
/// <param name="countOption">A mongodb counting option.</param>
/// <param name="partitionKey">An optional partition key.</param>
public virtual bool Any<TDocument, TKey>(FilterDefinition<TDocument> condition, CountOptions countOption = null,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var count = HandlePartitioned<TDocument, TKey>(partitionKey).CountDocuments(condition, countOption);
return count > 0;
}
/// <summary> /// <summary>
/// Returns true if any of the document of the collection matches the filter condition. /// Returns true if any of the document of the collection matches the filter condition.
/// </summary> /// </summary>
@@ -131,6 +199,39 @@ namespace MongoDbGenericRepository.DataAccess.Read
return (count > 0); 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="condition">A mongodb filter definition.</param>
/// <param name="findOption">A mongodb filter option.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual Task<List<TDocument>> GetAllAsync<TDocument, TKey>(FilterDefinition<TDocument> condition,
FindOptions findOption = null, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(condition, findOption).ToListAsync(cancellationToken);
}
/// <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="condition">A mongodb filter definition.</param>
/// <param name="findOption">A mongodb filter option.</param>
/// <param name="partitionKey">An optional partition key.</param>
public virtual List<TDocument> GetAll<TDocument, TKey>(FilterDefinition<TDocument> condition, FindOptions findOption = null,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(condition, findOption).ToList();
}
/// <summary> /// <summary>
/// Asynchronously returns a list of the documents matching the filter condition. /// Asynchronously returns a list of the documents matching the filter condition.
/// </summary> /// </summary>
@@ -160,6 +261,39 @@ namespace MongoDbGenericRepository.DataAccess.Read
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).ToList(); 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="condition">A mongodb filter definition.</param>
/// <param name="countOption">A mongodb counting option.</param>
/// <param name="partitionKey">An optional partitionKey</param>
/// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual Task<long> CountAsync<TDocument, TKey>(FilterDefinition<TDocument> condition, CountOptions countOption = null,
string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return HandlePartitioned<TDocument, TKey>(partitionKey).CountDocumentsAsync(condition, countOption, cancellationToken);
}
/// <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="condition">A mongodb filter definition.</param>
/// <param name="countOption">A mongodb counting option.</param>
/// <param name="partitionKey">An optional partitionKey</param>
public virtual long Count<TDocument, TKey>(FilterDefinition<TDocument> condition, CountOptions countOption = null,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return HandlePartitioned<TDocument, TKey>(partitionKey).CountDocuments(condition, countOption);
}
/// <summary> /// <summary>
/// Asynchronously counts how many documents match the filter condition. /// Asynchronously counts how many documents match the filter condition.
/// </summary> /// </summary>
@@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MongoDbGenericRepository namespace MongoDbGenericRepository
@@ -72,6 +72,35 @@ namespace MongoDbGenericRepository
return MongoDbReader.GetById<TDocument, TKey>(id, partitionKey); return MongoDbReader.GetById<TDocument, TKey>(id, partitionKey);
} }
/// <summary>
/// Asynchronously returns one document given filter 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="condition">A mongodb filter definition.</param>
/// <param name="findOption">A mongodb filter option.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">An optional cancellation Token.</param>
public Task<TDocument> GetOneAsync<TDocument, TKey>(FilterDefinition<TDocument> condition, FindOptions findOption = null, string partitionKey = null,
CancellationToken cancellationToken = default) where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>
{
return MongoDbReader.GetOneAsync<TDocument, TKey>(condition, findOption, partitionKey, cancellationToken);
}
/// <summary>
/// Returns one document given filter 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="condition">A mongodb filter definition.</param>
/// <param name="findOption">A mongodb filter option.</param>
/// <param name="partitionKey">An optional partition key.</param>
public TDocument GetOne<TDocument, TKey>(FilterDefinition<TDocument> condition, FindOptions findOption = null,
string partitionKey = null) where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>
{
return MongoDbReader.GetOne<TDocument, TKey>(condition, findOption, partitionKey);
}
/// <summary> /// <summary>
/// Asynchronously returns one document given an expression filter. /// Asynchronously returns one document given an expression filter.
/// </summary> /// </summary>
@@ -115,6 +144,34 @@ namespace MongoDbGenericRepository
return MongoDbReader.GetCursor<TDocument, TKey>(filter, partitionKey); 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>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="condition">A mongodb filter definition.</param>
/// <param name="countOption">A mongodb counting option.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">An optional cancellation Token.</param>
public Task<bool> AnyAsync<TDocument, TKey>(FilterDefinition<TDocument> condition, CountOptions countOption = null, string partitionKey = null,
CancellationToken cancellationToken = default) where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>
{
return MongoDbReader.AnyAsync<TDocument, TKey>(condition, countOption, partitionKey, cancellationToken);
}
/// <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="condition">A mongodb filter definition.</param>
/// <param name="countOption">A mongodb counting option.</param>
/// <param name="partitionKey">An optional partition key.</param>
public bool Any<TDocument, TKey>(FilterDefinition<TDocument> condition, CountOptions countOption = null, string partitionKey = null) where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>
{
return MongoDbReader.Any<TDocument, TKey>(condition, countOption, partitionKey);
}
/// <summary> /// <summary>
/// Returns true if any of the document of the collection matches the filter condition. /// Returns true if any of the document of the collection matches the filter condition.
/// </summary> /// </summary>
@@ -144,6 +201,35 @@ namespace MongoDbGenericRepository
return MongoDbReader.Any<TDocument, TKey>(filter, partitionKey); 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>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="condition">A mongodb filter definition.</param>
/// <param name="findOption">A mongodb filter option.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">An optional cancellation Token.</param>
public Task<List<TDocument>> GetAllAsync<TDocument, TKey>(FilterDefinition<TDocument> condition, FindOptions findOption = null, string partitionKey = null,
CancellationToken cancellationToken = default) where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>
{
return MongoDbReader.GetAllAsync<TDocument, TKey>(condition, findOption, partitionKey, cancellationToken);
}
/// <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="condition">A mongodb filter definition.</param>
/// <param name="findOption">A mongodb filter option.</param>
/// <param name="partitionKey">An optional partition key.</param>
public List<TDocument> GetAll<TDocument, TKey>(FilterDefinition<TDocument> condition, FindOptions findOption = null,
string partitionKey = null) where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>
{
return MongoDbReader.GetAll<TDocument, TKey>(condition, findOption, partitionKey);
}
/// <summary> /// <summary>
/// Asynchronously returns a list of the documents matching the filter condition. /// Asynchronously returns a list of the documents matching the filter condition.
/// </summary> /// </summary>
@@ -173,6 +259,35 @@ namespace MongoDbGenericRepository
return MongoDbReader.GetAll<TDocument, TKey>(filter, partitionKey); 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>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="condition">A mongodb filter definition.</param>
/// <param name="countOption">A mongodb counting option.</param>
/// <param name="partitionKey">An optional partitionKey</param>
/// <param name="cancellationToken">An optional cancellation Token.</param>
public Task<long> CountAsync<TDocument, TKey>(FilterDefinition<TDocument> condition, CountOptions countOption = null, string partitionKey = null,
CancellationToken cancellationToken = default) where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>
{
return MongoDbReader.CountAsync<TDocument, TKey>(condition, countOption, partitionKey, cancellationToken);
}
/// <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="condition">A mongodb filter definition.</param>
/// <param name="countOption">A mongodb counting option.</param>
/// <param name="partitionKey">An optional partitionKey</param>
public long Count<TDocument, TKey>(FilterDefinition<TDocument> condition, CountOptions countOption = null,
string partitionKey = null) where TDocument : IDocument<TKey> where TKey : IEquatable<TKey>
{
return MongoDbReader.Count<TDocument, TKey>(condition, countOption, partitionKey);
}
/// <summary> /// <summary>
/// Asynchronously counts how many documents match the filter condition. /// Asynchronously counts how many documents match the filter condition.
/// </summary> /// </summary>