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()
Inherited Members
Namespace: MongoDB.Entities
Assembly: MongoDB.Entities.dll
Syntax
public class Find<T, TProjection>
where T : IEntity
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
TProjection | The type you'd like to project the results to. |
Methods
ExecuteAnyAsync(CancellationToken)
Run the Find command and get back a bool indicating whether any entities matched the query
Declaration
public async Task<bool> ExecuteAnyAsync(CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<Boolean> |
ExecuteAsync(CancellationToken)
Run the Find command in MongoDB server and get a list of results
Declaration
public async Task<List<TProjection>> ExecuteAsync(CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<List<TProjection>> |
ExecuteCursorAsync(CancellationToken)
Run the Find command in MongoDB server and get a cursor instead of materialized results
Declaration
public Task<IAsyncCursor<TProjection>> ExecuteCursorAsync(CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<IAsyncCursor<TProjection>> |
ExecuteFirstAsync(CancellationToken)
Run the Find command in MongoDB server and get the first result or the default value if not found
Declaration
public async Task<TProjection> ExecuteFirstAsync(CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
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.
Declaration
public async Task<TProjection> ExecuteSingleAsync(CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<TProjection> |
IgnoreGlobalFilters()
Specify that this operation should ignore any global filters
Declaration
public Find<T, TProjection> IgnoreGlobalFilters()
Returns
Type | Description |
---|---|
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.
Declaration
public Find<T, TProjection> IncludeRequiredProps()
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Limit(Int32)
Specify how many entities to Take/Limit
Declaration
public Find<T, TProjection> Limit(int takeCount)
Parameters
Type | Name | Description |
---|---|---|
Int32 | takeCount | The number to limit/take |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
ManyAsync(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>, CancellationToken)
Find entities by supplying a filter expression
Declaration
public Task<List<TProjection>> ManyAsync(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> filter, CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> | filter | f => f.Eq(x => x.Prop, Value) & f.Gt(x => x.Prop, Value) |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<List<TProjection>> | A list of Entities |
ManyAsync(Expression<Func<T, Boolean>>, CancellationToken)
Find entities by supplying a lambda expression
Declaration
public Task<List<TProjection>> ManyAsync(Expression<Func<T, bool>> expression, CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<T, Boolean>> | expression | x => x.Property == Value |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<List<TProjection>> | A list of Entities |
Match(FilterDefinition<T>)
Specify the matching criteria with a filter definition
Declaration
public Find<T, TProjection> Match(FilterDefinition<T> filterDefinition)
Parameters
Type | Name | Description |
---|---|---|
FilterDefinition<T> | filterDefinition | A filter definition |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Match(Search, String, Boolean, Boolean, 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
Declaration
public Find<T, TProjection> Match(Search searchType, string searchTerm, bool caseSensitive = false, bool diacriticSensitive = false, string language = null)
Parameters
Type | Name | Description |
---|---|---|
Search | searchType | The type of text matching to do |
String | searchTerm | The search term |
Boolean | caseSensitive | Case sensitivity of the search (optional) |
Boolean | diacriticSensitive | Diacritic sensitivity of the search (optional) |
String | language | The language for the search (optional) |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Match(Template)
Specify the matching criteria with a template
Declaration
public Find<T, TProjection> Match(Template template)
Parameters
Type | Name | Description |
---|---|---|
Template | template | A Template with a find query |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Match(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>)
Specify the matching criteria with a filter expression
Declaration
public Find<T, TProjection> Match(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> filter)
Parameters
Type | Name | Description |
---|---|---|
Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> | filter | f => f.Eq(x => x.Prop, Value) & f.Gt(x => x.Prop, Value) |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Match(Expression<Func<T, Boolean>>)
Specify the matching criteria with a lambda expression
Declaration
public Find<T, TProjection> Match(Expression<Func<T, bool>> expression)
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<T, Boolean>> | expression | x => x.Property == Value |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Match(Expression<Func<T, Object>>, Coordinates2D, Nullable<Double>, Nullable<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
Declaration
public Find<T, TProjection> Match(Expression<Func<T, object>> coordinatesProperty, Coordinates2D nearCoordinates, double? maxDistance = null, double? minDistance = null)
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<T, Object>> | coordinatesProperty | The property where 2DCoordinates are stored |
Coordinates2D | nearCoordinates | The search point |
Nullable<Double> | maxDistance | Maximum distance in meters from the search point |
Nullable<Double> | minDistance | Minimum distance in meters from the search point |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Match(String)
Specify an IEntity ID as the matching criteria
Declaration
public Find<T, TProjection> Match(string ID)
Parameters
Type | Name | Description |
---|---|---|
String | ID | A unique IEntity ID |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
MatchExpression(Template)
Specify the matching criteria with a Template
Declaration
public Find<T, TProjection> MatchExpression(Template template)
Parameters
Type | Name | Description |
---|---|---|
Template | template | A Template object |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
MatchExpression(String)
Specify the matching criteria with an aggregation expression (i.e. $expr)
Declaration
public Find<T, TProjection> MatchExpression(string expression)
Parameters
Type | Name | Description |
---|---|---|
String | expression | { $gt: ['$Property1', '$Property2'] } |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
MatchID(String)
Specify an IEntity ID as the matching criteria
Declaration
public Find<T, TProjection> MatchID(string ID)
Parameters
Type | Name | Description |
---|---|---|
String | ID | A unique IEntity ID |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
MatchString(String)
Specify the matching criteria with a JSON string
Declaration
public Find<T, TProjection> MatchString(string jsonString)
Parameters
Type | Name | Description |
---|---|---|
String | jsonString | { Title : 'The Power Of Now' } |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
OneAsync(String, CancellationToken)
Find a single IEntity by ID
Declaration
public Task<TProjection> OneAsync(string ID, CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
String | ID | The unique ID of an IEntity |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
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)
Declaration
public Find<T, TProjection> Option(Action<FindOptions<T, TProjection>> option)
Parameters
Type | Name | Description |
---|---|---|
Action<FindOptions<T, TProjection>> | option | x => x.OptionName = OptionValue |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Project(Func<ProjectionDefinitionBuilder<T>, ProjectionDefinition<T, TProjection>>)
Specify how to project the results using a projection expression
Declaration
public Find<T, TProjection> Project(Func<ProjectionDefinitionBuilder<T>, ProjectionDefinition<T, TProjection>> projection)
Parameters
Type | Name | Description |
---|---|---|
Func<ProjectionDefinitionBuilder<T>, ProjectionDefinition<T, TProjection>> | projection | p => p.Include("Prop1").Exclude("Prop2") |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Project(Expression<Func<T, TProjection>>)
Specify how to project the results using a lambda expression
Declaration
public Find<T, TProjection> Project(Expression<Func<T, TProjection>> expression)
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<T, TProjection>> | expression | x => new Test { PropName = x.Prop } |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
ProjectExcluding(Expression<Func<T, Object>>)
Specify how to project the results using an exclusion projection expression.
Declaration
public Find<T, TProjection> ProjectExcluding(Expression<Func<T, object>> exclusion)
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<T, Object>> | exclusion | x => new { x.PropToExclude, x.AnotherPropToExclude } |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Skip(Int32)
Specify how many entities to skip
Declaration
public Find<T, TProjection> Skip(int skipCount)
Parameters
Type | Name | Description |
---|---|---|
Int32 | skipCount | The number to skip |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Sort(Func<SortDefinitionBuilder<T>, SortDefinition<T>>)
Specify how to sort using a sort expression
Declaration
public Find<T, TProjection> Sort(Func<SortDefinitionBuilder<T>, SortDefinition<T>> sortFunction)
Parameters
Type | Name | Description |
---|---|---|
Func<SortDefinitionBuilder<T>, SortDefinition<T>> | sortFunction | s => s.Ascending("Prop1").MetaTextScore("Prop2") |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Sort(Expression<Func<T, Object>>, Order)
Specify which property and order to use for sorting (use multiple times if needed)
Declaration
public Find<T, TProjection> Sort(Expression<Func<T, object>> propertyToSortBy, Order sortOrder)
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<T, Object>> | propertyToSortBy | x => x.Prop |
Order | sortOrder | The sort order |
Returns
Type | Description |
---|---|
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
Declaration
public Find<T, TProjection> SortByTextScore()
Returns
Type | Description |
---|---|
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
Declaration
public Find<T, TProjection> SortByTextScore(Expression<Func<T, object>> scoreProperty)
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<T, Object>> | scoreProperty | x => x.TextScoreProp |
Returns
Type | Description |
---|---|
Find<T, TProjection> |