Table of Contents

Class Find<T, TProjection>

Namespace
MongoDB.Entities
Assembly
MongoDB.Entities.dll

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

T

Any class that implements IEntity

TProjection

The 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

cancellation CancellationToken

An optional cancellation token

Returns

Task<bool>

ExecuteAsync(CancellationToken)

Run the Find command in MongoDB server and get a list of results

public Task<List<TProjection>> ExecuteAsync(CancellationToken cancellation = default)

Parameters

cancellation CancellationToken

An optional cancellation token

Returns

Task<List<TProjection>>

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

cancellation CancellationToken

An 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

cancellation CancellationToken

An 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

cancellation CancellationToken

An 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

takeCount int

The 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

filter Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>

f => f.Eq(x => x.Prop, Value) & f.Gt(x => x.Prop, Value)

cancellation CancellationToken

An optional cancellation token

Returns

Task<List<TProjection>>

A list of Entities

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

expression Expression<Func<T, bool>>

x => x.Property == Value

cancellation CancellationToken

An optional cancellation token

Returns

Task<List<TProjection>>

A list of Entities

Match(FilterDefinition<T>)

Specify the matching criteria with a filter definition

public Find<T, TProjection> Match(FilterDefinition<T> filterDefinition)

Parameters

filterDefinition FilterDefinition<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

searchType Search

The type of text matching to do

searchTerm string

The search term

caseSensitive bool

Case sensitivity of the search (optional)

diacriticSensitive bool

Diacritic sensitivity of the search (optional)

language string

The 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

template Template

A 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

filter Func<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

expression Expression<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

coordinatesProperty Expression<Func<T, object>>

The property where 2DCoordinates are stored

nearCoordinates Coordinates2D

The search point

maxDistance double?

Maximum distance in meters from the search point

minDistance double?

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

ID object

A unique IEntity ID

Returns

Find<T, TProjection>

MatchExpression(Template)

Specify the matching criteria with a Template

public Find<T, TProjection> MatchExpression(Template template)

Parameters

template Template

A 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

expression string

{ $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

ID object

A 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

jsonString string

{ 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

ID object

The unique ID of an IEntity

cancellation CancellationToken

An 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

option Action<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

projection Func<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

expression Expression<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

exclusion Expression<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

skipCount int

The 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

sortFunction Func<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

propertyToSortBy Expression<Func<T, object>>

x => x.Prop

sortOrder Order

The 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

scoreProperty Expression<Func<T, object>>

x => x.TextScoreProp

Returns

Find<T, TProjection>