637 lines
33 KiB
C#
637 lines
33 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 class ReadOnlyMongoRepository : 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>
|
|
/// 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)
|
|
{
|
|
MongoDbContext = new MongoDbContext(connectionString, databaseName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The contructor taking a <see cref="IMongoDbContext"/>.
|
|
/// </summary>
|
|
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
|
protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext)
|
|
{
|
|
MongoDbContext = mongoDbContext;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The contructor taking a <see cref="IMongoDatabase"/>.
|
|
/// </summary>
|
|
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
|
protected ReadOnlyMongoRepository(IMongoDatabase 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="orderByDescending">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>> orderByDescending, string partitionKey = null)
|
|
where TDocument : IDocument
|
|
{
|
|
return await GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
|
.SortByDescending(orderByDescending)
|
|
.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>
|
|
/// <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>
|
|
public TDocument GetByMax<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
|
where TDocument : IDocument
|
|
{
|
|
return GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
|
.SortByDescending(orderByDescending)
|
|
.FirstOrDefault();
|
|
}
|
|
|
|
/// <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 async Task<TDocument> GetByMinAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
|
where TDocument : IDocument
|
|
{
|
|
return await GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
|
.SortBy(orderByAscending)
|
|
.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>
|
|
/// <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 GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
|
.SortBy(orderByAscending)
|
|
.FirstOrDefault();
|
|
}
|
|
|
|
#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>
|
|
public async 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 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 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 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 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 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 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 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 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 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 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();
|
|
}
|
|
|
|
/// <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>
|
|
public async 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>
|
|
{
|
|
return await GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
|
.SortByDescending(orderByDescending)
|
|
.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="orderByAscending">A property selector to order by descending.</param>
|
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
public 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>
|
|
{
|
|
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
|
.SortByDescending(orderByDescending)
|
|
.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="orderByAscending">A property selector to order by ascending.</param>
|
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
public async 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>
|
|
{
|
|
return await GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
|
.SortBy(orderByAscending)
|
|
.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="orderByAscending">A property selector to order by ascending.</param>
|
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
public 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>
|
|
{
|
|
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
|
.SortBy(orderByAscending)
|
|
.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 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 GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
|
.SortByDescending(ConvertExpression(maxValueSelector))
|
|
.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>
|
|
/// <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 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>
|
|
{
|
|
|
|
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
|
.SortByDescending(ConvertExpression(orderByDescending))
|
|
.Project(orderByDescending)
|
|
.FirstOrDefault();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Utility Methods
|
|
|
|
/// <summary>
|
|
/// Gets a collections for the type TDocument with the matching partition key.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
/// <param name="partitionKey">The partion key.</param>
|
|
/// <returns>An <see cref="IMongoCollection{TDocument}"/></returns>
|
|
protected IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument
|
|
{
|
|
return MongoDbContext.GetCollection<TDocument>(partitionKey);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a collections for the type TDocument
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
/// <returns></returns>
|
|
protected IMongoCollection<TDocument> GetCollection<TDocument>() where TDocument : IDocument
|
|
{
|
|
return MongoDbContext.GetCollection<TDocument>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a collections for the type TDocument
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
/// <param name="document">The document.</param>
|
|
/// <returns></returns>
|
|
protected IMongoCollection<TDocument> HandlePartitioned<TDocument>(TDocument document) where TDocument : IDocument
|
|
{
|
|
if (document is IPartitionedDocument)
|
|
{
|
|
return GetCollection<TDocument>(((IPartitionedDocument)document).PartitionKey);
|
|
}
|
|
return GetCollection<TDocument>();
|
|
}
|
|
|
|
/// <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>
|
|
protected 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 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>
|
|
/// 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>
|
|
protected 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 static 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);
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|