Table of Contents

Class PagedSearch<T, TProjection>

Namespace
MongoDB.Entities
Assembly
MongoDB.Entities.dll

Represents an aggregation query that retrieves results with easy paging support.

public class PagedSearch<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
PagedSearch<T, TProjection>
Derived
Inherited Members

Methods

ExecuteAsync(CancellationToken)

Run the aggregation search command in MongoDB server and get a page of results and total + page count

public Task<(IReadOnlyList<TProjection> Results, long TotalCount, int PageCount)> ExecuteAsync(CancellationToken cancellation = default)

Parameters

cancellation CancellationToken

An optional cancellation token

Returns

Task<(IReadOnlyList<TProjection> Results, long TotalCount, int PageCount)>

IgnoreGlobalFilters()

Specify that this operation should ignore any global filters

public PagedSearch<T, TProjection> IgnoreGlobalFilters()

Returns

PagedSearch<T, TProjection>

Match(FilterDefinition<T>)

Specify the matching criteria with a filter definition

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

Parameters

filterDefinition FilterDefinition<T>

A filter definition

Returns

PagedSearch<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 PagedSearch<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

PagedSearch<T, TProjection>

Match(Template)

Specify the matching criteria with a template

public PagedSearch<T, TProjection> Match(Template template)

Parameters

template Template

A Template with a find query

Returns

PagedSearch<T, TProjection>

Match(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>)

Specify the matching criteria with a filter expression

public PagedSearch<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

PagedSearch<T, TProjection>

Match(Expression<Func<T, bool>>)

Specify the matching criteria with a lambda expression

public PagedSearch<T, TProjection> Match(Expression<Func<T, bool>> expression)

Parameters

expression Expression<Func<T, bool>>

x => x.Property == Value

Returns

PagedSearch<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 PagedSearch<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

PagedSearch<T, TProjection>

MatchExpression(Template)

Specify the matching criteria with a Template

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

Parameters

template Template

A Template object

Returns

PagedSearch<T, TProjection>

MatchExpression(string)

Specify the matching criteria with an aggregation expression (i.e. $expr)

public PagedSearch<T, TProjection> MatchExpression(string expression)

Parameters

expression string

{ $gt: ['$Property1', '$Property2'] }

Returns

PagedSearch<T, TProjection>

MatchString(string)

Specify the matching criteria with a JSON string

public PagedSearch<T, TProjection> MatchString(string jsonString)

Parameters

jsonString string

{ Title : 'The Power Of Now' }

Returns

PagedSearch<T, TProjection>

Option(Action<AggregateOptions>)

Specify an option for this find command (use multiple times if needed)

public PagedSearch<T, TProjection> Option(Action<AggregateOptions> option)

Parameters

option Action<AggregateOptions>

x => x.OptionName = OptionValue

Returns

PagedSearch<T, TProjection>

PageNumber(int)

Specify the page number to get

public PagedSearch<T, TProjection> PageNumber(int pageNumber)

Parameters

pageNumber int

The page number

Returns

PagedSearch<T, TProjection>

PageSize(int)

Specify the number of items per page

public PagedSearch<T, TProjection> PageSize(int pageSize)

Parameters

pageSize int

The size of a page

Returns

PagedSearch<T, TProjection>

Project(Func<ProjectionDefinitionBuilder<T>, ProjectionDefinition<T, TProjection>>)

Specify how to project the results using a projection expression

public PagedSearch<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

PagedSearch<T, TProjection>

Project(Expression<Func<T, TProjection>>)

Specify how to project the results using a lambda expression

public PagedSearch<T, TProjection> Project(Expression<Func<T, TProjection>> expression)

Parameters

expression Expression<Func<T, TProjection>>

x => new Test { PropName = x.Prop }

Returns

PagedSearch<T, TProjection>

ProjectExcluding(Expression<Func<T, object?>>)

Specify how to project the results using an exclusion projection expression.

public PagedSearch<T, TProjection> ProjectExcluding(Expression<Func<T, object?>> exclusion)

Parameters

exclusion Expression<Func<T, object>>

x => new { x.PropToExclude, x.AnotherPropToExclude }

Returns

PagedSearch<T, TProjection>

Sort(Func<SortDefinitionBuilder<T>, SortDefinition<T>>)

Specify how to sort using a sort expression

public PagedSearch<T, TProjection> Sort(Func<SortDefinitionBuilder<T>, SortDefinition<T>> sortFunction)

Parameters

sortFunction Func<SortDefinitionBuilder<T>, SortDefinition<T>>

s => s.Ascending("Prop1").MetaTextScore("Prop2")

Returns

PagedSearch<T, TProjection>

Sort(Expression<Func<T, object?>>, Order)

Specify which property and order to use for sorting (use multiple times if needed)

public PagedSearch<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

PagedSearch<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 PagedSearch<T, TProjection> SortByTextScore()

Returns

PagedSearch<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 PagedSearch<T, TProjection> SortByTextScore(Expression<Func<T, object?>>? scoreProperty)

Parameters

scoreProperty Expression<Func<T, object>>

x => x.TextScoreProp

Returns

PagedSearch<T, TProjection>

WithFluent<TFluent>(TFluent)

Begins the paged search aggregation pipeline with the provided fluent pipeline.

TIP: This method must be first in the chain, and it cannot be used with .Match()

public PagedSearch<T, TProjection> WithFluent<TFluent>(TFluent fluentPipeline) where TFluent : IAggregateFluent<T>

Parameters

fluentPipeline TFluent

The input IAggregateFluent pipeline

Returns

PagedSearch<T, TProjection>

Type Parameters

TFluent

The type of the input pipeline