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;
using System.Threading.Tasks;
namespace MongoDbGenericRepository.DataAccess.Read
{
public partial class MongoDbReader : IMongoDbReader
{
///
/// Asynchronously returns a projected document matching the filter condition.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The type representing the model you want to project to.
/// A LINQ expression filter.
/// The projection expression.
/// An optional partition key.
/// An optional cancellation Token.
public virtual async Task ProjectOneAsync(
Expression> filter,
Expression> projection,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument
where TKey : IEquatable
where TProjection : class
{
return await HandlePartitioned(partitionKey).Find(filter)
.Project(projection)
.FirstOrDefaultAsync(cancellationToken);
}
///
/// Returns a projected document matching the filter condition.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The type representing the model you want to project to.
/// A LINQ expression filter.
/// The projection expression.
/// An optional partition key.
public virtual TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
where TProjection : class
{
return HandlePartitioned(partitionKey).Find(filter)
.Project(projection)
.FirstOrDefault();
}
///
/// Asynchronously returns a list of projected documents matching the filter condition.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The type representing the model you want to project to.
/// A LINQ expression filter.
/// The projection expression.
/// An optional partition key.
/// An optional cancellation Token.
public virtual async Task> ProjectManyAsync(
Expression> filter,
Expression> projection,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument
where TKey : IEquatable
where TProjection : class
{
return await HandlePartitioned(partitionKey).Find(filter)
.Project(projection)
.ToListAsync(cancellationToken);
}
///
/// Asynchronously returns a list of projected documents matching the filter condition.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The type representing the model you want to project to.
/// The document filter.
/// The projection expression.
/// An optional partition key.
public virtual List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
where TProjection : class
{
return HandlePartitioned(partitionKey).Find(filter)
.Project(projection)
.ToList();
}
}
}