243 lines
13 KiB
C#
243 lines
13 KiB
C#
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 ReadOnlyMongoRepository implements the readonly functionality of the IReadOnlyMongoRepository.
|
|
/// </summary>
|
|
public abstract partial class ReadOnlyMongoRepository : IReadOnlyMongoRepository
|
|
{
|
|
|
|
|
|
#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);
|
|
}
|
|
|
|
/// <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>
|
|
/// 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>
|
|
/// 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
|
|
{
|
|
return GetMinValue<TDocument, Guid, TValue>(filter, minValueSelector, partitionKey);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|