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
{
///
/// The ReadOnlyMongoRepository implements the readonly functionality of the IReadOnlyMongoRepository.
///
public abstract partial class ReadOnlyMongoRepository : IReadOnlyMongoRepository
{
#region Read
///
/// Asynchronously returns one document given its id.
///
/// The type representing a Document.
/// The Id of the document you want to get.
/// An optional partition key.
public async Task GetByIdAsync(Guid id, string partitionKey = null) where TDocument : IDocument
{
var filter = Builders.Filter.Eq("Id", id);
return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync();
}
///
/// Returns one document given its id.
///
/// The type representing a Document.
/// The Id of the document you want to get.
/// An optional partition key.
public TDocument GetById(Guid id, string partitionKey = null) where TDocument : IDocument
{
var filter = Builders.Filter.Eq("Id", id);
return HandlePartitioned(partitionKey).Find(filter).FirstOrDefault();
}
///
/// Asynchronously returns one document given an expression filter.
///
/// The type representing a Document.
/// A LINQ expression filter.
/// An optional partition key.
public async Task GetOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument
{
return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync();
}
///
/// Returns one document given an expression filter.
///
/// The type representing a Document.
/// A LINQ expression filter.
/// An optional partition key.
public TDocument GetOne(Expression> filter, string partitionKey = null) where TDocument : IDocument
{
return HandlePartitioned(partitionKey).Find(filter).FirstOrDefault();
}
///
/// Returns a collection cursor.
///
/// The type representing a Document.
/// A LINQ expression filter.
/// An optional partition key.
public IFindFluent GetCursor(Expression> filter, string partitionKey = null) where TDocument : IDocument
{
return HandlePartitioned(partitionKey).Find(filter);
}
///
/// Returns true if any of the document of the collection matches the filter condition.
///
/// The type representing a Document.
/// A LINQ expression filter.
/// An optional partition key.
public async Task AnyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument
{
var count = await HandlePartitioned(partitionKey).CountDocumentsAsync(filter);
return (count > 0);
}
///
/// Returns true if any of the document of the collection matches the filter condition.
///
/// The type representing a Document.
/// A LINQ expression filter.
/// An optional partition key.
public bool Any(Expression> filter, string partitionKey = null) where TDocument : IDocument
{
var count = HandlePartitioned(partitionKey).CountDocuments(filter);
return (count > 0);
}
///
/// Asynchronously returns a list of the documents matching the filter condition.
///
/// The type representing a Document.
/// A LINQ expression filter.
/// An optional partition key.
public async Task> GetAllAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument
{
return await HandlePartitioned(partitionKey).Find(filter).ToListAsync();
}
///
/// Returns a list of the documents matching the filter condition.
///
/// The type representing a Document.
/// A LINQ expression filter.
/// An optional partition key.
public List GetAll(Expression> filter, string partitionKey = null) where TDocument : IDocument
{
return HandlePartitioned(partitionKey).Find(filter).ToList();
}
///
/// Asynchronously counts how many documents match the filter condition.
///
/// The type representing a Document.
/// A LINQ expression filter.
/// An optional partitionKey
public async Task CountAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument
{
return await HandlePartitioned(partitionKey).CountDocumentsAsync(filter);
}
///
/// Counts how many documents match the filter condition.
///
/// The type representing a Document.
/// A LINQ expression filter.
/// An optional partitionKey
public long Count(Expression> filter, string partitionKey = null) where TDocument : IDocument
{
return HandlePartitioned(partitionKey).Find(filter).CountDocuments();
}
///
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
///
/// The document type.
/// A LINQ expression filter.
/// A property selector to order by descending.
/// An optional partitionKey.
public async Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null)
where TDocument : IDocument
{
return await GetByMaxAsync(filter, maxValueSelector, partitionKey);
}
///
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
///
/// The document type.
/// A LINQ expression filter.
/// A property selector to order by descending.
/// An optional partitionKey.
///
public TDocument GetByMax(Expression> filter, Expression> maxValueSelector, string partitionKey = null)
where TDocument : IDocument
{
return GetByMax(filter, maxValueSelector, partitionKey);
}
///
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
///
/// The document type.
/// A LINQ expression filter.
/// A property selector to order by ascending.
/// An optional partitionKey.
public async Task GetByMinAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null)
where TDocument : IDocument
{
return await GetByMinAsync(filter, minValueSelector, partitionKey);
}
///
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
///
/// The document type.
/// A LINQ expression filter.
/// A property selector to order by ascending.
/// An optional partitionKey.
public TDocument GetByMin(Expression> filter, Expression> orderByAscending, string partitionKey = null)
where TDocument : IDocument
{
return GetByMin(filter, orderByAscending, partitionKey);
}
///
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
///
/// The document type.
/// The type of the value used to order the query.
/// A LINQ expression filter.
/// A property selector to order by ascending.
/// An optional partitionKey.
public async Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null)
where TDocument : IDocument
{
return await GetMaxValueAsync(filter, maxValueSelector, partitionKey);
}
///
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
///
/// The document type.
/// The type of the value used to order the query.
/// A LINQ expression filter.
/// A property selector to order by ascending.
/// An optional partitionKey.
public TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey = null)
where TDocument : IDocument
{
return GetMaxValue(filter, maxValueSelector, partitionKey);
}
///
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
///
/// The document type.
/// The type of the value used to order the query.
/// A LINQ expression filter.
/// A property selector to order by ascending.
/// An optional partition key.
public virtual TValue GetMinValue(Expression> filter, Expression> minValueSelector, string partitionKey = null)
where TDocument : IDocument
{
return GetMinValue(filter, minValueSelector, partitionKey);
}
#endregion
}
}