Class Find<T, TProjection>
Represents a MongoDB Find command with the ability to project to a different result type.
TIP: Specify your criteria using .Match() .Sort() .Skip() .Take() .Project() .Option() methods and finally call .Execute()
public class Find<T, TProjection> where T : IEntity
Type Parameters
TAny class that implements IEntity
TProjectionThe type you'd like to project the results to.
- Inheritance
-
Find<T, TProjection>
- Derived
- Inherited Members
Methods
ExecuteAnyAsync(CancellationToken)
Run the Find command and get back a bool indicating whether any entities matched the query
public Task<bool> ExecuteAnyAsync(CancellationToken cancellation = default)
Parameters
cancellationCancellationTokenAn optional cancellation token
Returns
ExecuteAsync(CancellationToken)
Run the Find command in MongoDB server and get a list of results
public Task<List<TProjection>> ExecuteAsync(CancellationToken cancellation = default)
Parameters
cancellationCancellationTokenAn optional cancellation token
Returns
ExecuteCursorAsync(CancellationToken)
Run the Find command in MongoDB server and get a cursor instead of materialized results
public Task<IAsyncCursor<TProjection>> ExecuteCursorAsync(CancellationToken cancellation = default)
Parameters
cancellationCancellationTokenAn optional cancellation token
Returns
- Task<IAsyncCursor<TProjection>>
ExecuteFirstAsync(CancellationToken)
Run the Find command in MongoDB server and get the first result or the default value if not found
public Task<TProjection?> ExecuteFirstAsync(CancellationToken cancellation = default)
Parameters
cancellationCancellationTokenAn optional cancellation token
Returns
- Task<TProjection>
ExecuteSingleAsync(CancellationToken)
Run the Find command in MongoDB server and get a single result or the default value if not found. If more than one entity is found, it will throw an exception.
public Task<TProjection?> ExecuteSingleAsync(CancellationToken cancellation = default)
Parameters
cancellationCancellationTokenAn optional cancellation token
Returns
- Task<TProjection>
IgnoreGlobalFilters()
Specify that this operation should ignore any global filters
public Find<T, TProjection> IgnoreGlobalFilters()
Returns
- Find<T, TProjection>
IncludeRequiredProps()
Specify to automatically include all properties marked with [BsonRequired] attribute on the entity in the final projection.
HINT: this method should only be called after the .Project() method.
public Find<T, TProjection> IncludeRequiredProps()
Returns
- Find<T, TProjection>
Limit(int)
Specify how many entities to Take/Limit
public Find<T, TProjection> Limit(int takeCount)
Parameters
takeCountintThe number to limit/take
Returns
- Find<T, TProjection>
ManyAsync(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>, CancellationToken)
Find entities by supplying a filter expression
public Task<List<TProjection>> ManyAsync(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> filter, CancellationToken cancellation = default)
Parameters
filterFunc<FilterDefinitionBuilder<T>, FilterDefinition<T>>f => f.Eq(x => x.Prop, Value) & f.Gt(x => x.Prop, Value)
cancellationCancellationTokenAn optional cancellation token
Returns
ManyAsync(Expression<Func<T, bool>>, CancellationToken)
Find entities by supplying a lambda expression
public Task<List<TProjection>> ManyAsync(Expression<Func<T, bool>> expression, CancellationToken cancellation = default)
Parameters
expressionExpression<Func<T, bool>>x => x.Property == Value
cancellationCancellationTokenAn optional cancellation token
Returns
Match(FilterDefinition<T>)
Specify the matching criteria with a filter definition
public Find<T, TProjection> Match(FilterDefinition<T> filterDefinition)
Parameters
filterDefinitionFilterDefinition<T>A filter definition
Returns
- Find<T, TProjection>
Match(Search, string, bool, bool, string?)
Specify a search term to find results from the text index of this particular collection.
TIP: Make sure to define a text index with DB.Index<T>() before searching
public Find<T, TProjection> Match(Search searchType, string searchTerm, bool caseSensitive = false, bool diacriticSensitive = false, string? language = null)
Parameters
searchTypeSearchThe type of text matching to do
searchTermstringThe search term
caseSensitiveboolCase sensitivity of the search (optional)
diacriticSensitiveboolDiacritic sensitivity of the search (optional)
languagestringThe language for the search (optional)
Returns
- Find<T, TProjection>
Match(Template)
Specify the matching criteria with a template
public Find<T, TProjection> Match(Template template)
Parameters
templateTemplateA Template with a find query
Returns
- Find<T, TProjection>
Match(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>)
Specify the matching criteria with a filter expression
public Find<T, TProjection> Match(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> filter)
Parameters
filterFunc<FilterDefinitionBuilder<T>, FilterDefinition<T>>f => f.Eq(x => x.Prop, Value) & f.Gt(x => x.Prop, Value)
Returns
- Find<T, TProjection>
Match(Expression<Func<T, bool>>)
Specify the matching criteria with a lambda expression
public Find<T, TProjection> Match(Expression<Func<T, bool>> expression)
Parameters
expressionExpression<Func<T, bool>>x => x.Property == Value
Returns
- Find<T, TProjection>
Match(Expression<Func<T, object?>>, Coordinates2D, double?, double?)
Specify criteria for matching entities based on GeoSpatial data (longitude & latitude)
TIP: Make sure to define a Geo2DSphere index with DB.Index<T>() before searching
Note: DB.FluentGeoNear() supports more advanced options
public Find<T, TProjection> Match(Expression<Func<T, object?>> coordinatesProperty, Coordinates2D nearCoordinates, double? maxDistance = null, double? minDistance = null)
Parameters
coordinatesPropertyExpression<Func<T, object>>The property where 2DCoordinates are stored
nearCoordinatesCoordinates2DThe search point
maxDistancedouble?Maximum distance in meters from the search point
minDistancedouble?Minimum distance in meters from the search point
Returns
- Find<T, TProjection>
Match(object)
Specify an IEntity ID as the matching criteria
public Find<T, TProjection> Match(object ID)
Parameters
IDobjectA unique IEntity ID
Returns
- Find<T, TProjection>
MatchExpression(Template)
Specify the matching criteria with a Template
public Find<T, TProjection> MatchExpression(Template template)
Parameters
templateTemplateA Template object
Returns
- Find<T, TProjection>
MatchExpression(string)
Specify the matching criteria with an aggregation expression (i.e. $expr)
public Find<T, TProjection> MatchExpression(string expression)
Parameters
expressionstring{ $gt: ['$Property1', '$Property2'] }
Returns
- Find<T, TProjection>
MatchID(object)
Specify an IEntity ID as the matching criteria
public Find<T, TProjection> MatchID(object ID)
Parameters
IDobjectA unique IEntity ID
Returns
- Find<T, TProjection>
MatchString(string)
Specify the matching criteria with a JSON string
public Find<T, TProjection> MatchString(string jsonString)
Parameters
jsonStringstring{ Title : 'The Power Of Now' }
Returns
- Find<T, TProjection>
OneAsync(object, CancellationToken)
Find a single IEntity by ID
public Task<TProjection?> OneAsync(object ID, CancellationToken cancellation = default)
Parameters
IDobjectThe unique ID of an IEntity
cancellationCancellationTokenAn optional cancellation token
Returns
- Task<TProjection>
A single entity or null if not found
Option(Action<FindOptions<T, TProjection>>)
Specify an option for this find command (use multiple times if needed)
public Find<T, TProjection> Option(Action<FindOptions<T, TProjection>> option)
Parameters
optionAction<FindOptions<T, TProjection>>x => x.OptionName = OptionValue
Returns
- Find<T, TProjection>
Project(Func<ProjectionDefinitionBuilder<T>, ProjectionDefinition<T, TProjection>>)
Specify how to project the results using a projection expression
public Find<T, TProjection> Project(Func<ProjectionDefinitionBuilder<T>, ProjectionDefinition<T, TProjection>> projection)
Parameters
projectionFunc<ProjectionDefinitionBuilder<T>, ProjectionDefinition<T, TProjection>>p => p.Include("Prop1").Exclude("Prop2")
Returns
- Find<T, TProjection>
Project(Expression<Func<T, TProjection>>)
Specify how to project the results using a lambda expression
public Find<T, TProjection> Project(Expression<Func<T, TProjection>> expression)
Parameters
expressionExpression<Func<T, TProjection>>x => new Test { PropName = x.Prop }
Returns
- Find<T, TProjection>
ProjectExcluding(Expression<Func<T, object>>)
Specify how to project the results using an exclusion projection expression.
public Find<T, TProjection> ProjectExcluding(Expression<Func<T, object>> exclusion)
Parameters
exclusionExpression<Func<T, object>>x => new { x.PropToExclude, x.AnotherPropToExclude }
Returns
- Find<T, TProjection>
Skip(int)
Specify how many entities to skip
public Find<T, TProjection> Skip(int skipCount)
Parameters
skipCountintThe number to skip
Returns
- Find<T, TProjection>
Sort(Func<SortDefinitionBuilder<T>, SortDefinition<T>>)
Specify how to sort using a sort expression
public Find<T, TProjection> Sort(Func<SortDefinitionBuilder<T>, SortDefinition<T>> sortFunction)
Parameters
sortFunctionFunc<SortDefinitionBuilder<T>, SortDefinition<T>>s => s.Ascending("Prop1").MetaTextScore("Prop2")
Returns
- Find<T, TProjection>
Sort(Expression<Func<T, object?>>, Order)
Specify which property and order to use for sorting (use multiple times if needed)
public Find<T, TProjection> Sort(Expression<Func<T, object?>> propertyToSortBy, Order sortOrder)
Parameters
propertyToSortByExpression<Func<T, object>>x => x.Prop
sortOrderOrderThe sort order
Returns
- Find<T, TProjection>
SortByTextScore()
Sort the results of a text search by the MetaTextScore
TIP: Use this method after .Project() if you need to do a projection also
public Find<T, TProjection> SortByTextScore()
Returns
- Find<T, TProjection>
SortByTextScore(Expression<Func<T, object?>>?)
Sort the results of a text search by the MetaTextScore and get back the score as well
TIP: Use this method after .Project() if you need to do a projection also
public Find<T, TProjection> SortByTextScore(Expression<Func<T, object?>>? scoreProperty)
Parameters
scorePropertyExpression<Func<T, object>>x => x.TextScoreProp
Returns
- Find<T, TProjection>