Table of Contents

Class Extensions

Namespace
MongoDB.Entities
Assembly
MongoDB.Entities.dll

Extension methods for entities

public static class Extensions
Inheritance
Extensions
Inherited Members

Methods

DeleteAllAsync<T>(IEnumerable<T>, DB, CancellationToken)

Deletes multiple entities from the database

HINT: If these entities are referenced by one-to-many/many-to-many relationships, those references are also deleted.

public static Task<DeleteResult> DeleteAllAsync<T>(this IEnumerable<T> entities, DB db, CancellationToken cancellation = default) where T : IEntity

Parameters

entities IEnumerable<T>
db DB

The DB instance to use for this operation

cancellation CancellationToken

An optional cancellation token

Returns

Task<DeleteResult>

Type Parameters

T

DeleteAsync<T>(T, DB, CancellationToken)

Deletes a single entity from MongoDB.

HINT: If this entity is referenced by one-to-many/many-to-many relationships, those references are also deleted.

public static Task<DeleteResult> DeleteAsync<T>(this T entity, DB db, CancellationToken cancellation = default) where T : IEntity

Parameters

entity T
db DB

The DB instance to use for this operation

cancellation CancellationToken

An optional cancellation token

Returns

Task<DeleteResult>

Type Parameters

T

Distinct<T>(IAggregateFluent<T>)

Adds a distinct aggregation stage to a fluent pipeline.

public static IAggregateFluent<T> Distinct<T>(this IAggregateFluent<T> aggregate) where T : IEntity

Parameters

aggregate IAggregateFluent<T>

Returns

IAggregateFluent<T>

Type Parameters

T

Any class that implements IEntity

DropAsync(IMongoCollection<JoinRecord>)

Drops a join collection

public static Task DropAsync(this IMongoCollection<JoinRecord> collection)

Parameters

collection IMongoCollection<JoinRecord>

Returns

Task

ExistsAsync(IMongoDatabase, int)

Checks to see if the database already exists on the mongodb server

public static Task<bool> ExistsAsync(this IMongoDatabase db, int timeoutSeconds = 5)

Parameters

db IMongoDatabase
timeoutSeconds int

The number of seconds to keep trying

Returns

Task<bool>

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

Returns the full dotted path of a property for the given expression

public static string FullPath<T>(this Expression<Func<T, object?>> expression)

Parameters

expression Expression<Func<T, object>>

Returns

string

Type Parameters

T

Any class that implements IEntity

InitManyToMany<TChild, TParent>(IEntity, Expression<Func<Many<TChild, TParent>?>>, Expression<Func<TChild, object>?>, DB?)

Initializes supplied property with a new Many-To-Many relationship.

public static void InitManyToMany<TChild, TParent>(this IEntity parent, Expression<Func<Many<TChild, TParent>?>> propertyToInit, Expression<Func<TChild, object>?> propertyOtherSide, DB? db = null) where TChild : IEntity where TParent : IEntity

Parameters

parent IEntity
propertyToInit Expression<Func<Many<TChild, TParent>>>

() = > PropertyName

propertyOtherSide Expression<Func<TChild, object>>

x => x.PropertyName

db DB

the DB instance to work with

Type Parameters

TChild
TParent

InitOneToMany<TChild, TParent>(TParent, Expression<Func<Many<TChild, TParent>?>>, DB?)

Initializes supplied property with a new One-To-Many relationship.

public static void InitOneToMany<TChild, TParent>(this TParent parent, Expression<Func<Many<TChild, TParent>?>> propertyToInit, DB? db = null) where TChild : IEntity where TParent : IEntity

Parameters

parent TParent
propertyToInit Expression<Func<Many<TChild, TParent>>>

() => PropertyName

db DB

the database instance to work with

Type Parameters

TChild
TParent

IsAccessibleAsync(IMongoDatabase, int)

Pings the mongodb server to check if it's still connectable

public static Task<bool> IsAccessibleAsync(this IMongoDatabase db, int timeoutSeconds = 5)

Parameters

db IMongoDatabase
timeoutSeconds int

The number of seconds to keep trying

Returns

Task<bool>

MatchExpression<T>(IAggregateFluent<T>, string)

Appends a match stage to the pipeline with an aggregation expression (i.e. $expr)

public static IAggregateFluent<T> MatchExpression<T>(this IAggregateFluent<T> aggregate, string expression) where T : IEntity

Parameters

aggregate IAggregateFluent<T>
expression string

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

Returns

IAggregateFluent<T>

Type Parameters

T

Any class that implements IEntity

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

Appends a match stage to the pipeline with a filter expression

public static IAggregateFluent<T> Match<T>(this IAggregateFluent<T> aggregate, Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> filter) where T : IEntity

Parameters

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

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

Returns

IAggregateFluent<T>

Type Parameters

T

Any class that implements IEntity

SortByRelevance<T>(IEnumerable<T>, string, Func<T, string>, int?)

Sort a list of objects by relevance to a given string using Levenshtein Distance

public static IEnumerable<T> SortByRelevance<T>(this IEnumerable<T> objects, string searchTerm, Func<T, string> propertyToSortBy, int? maxDistance = null)

Parameters

objects IEnumerable<T>

The list of objects to sort

searchTerm string

The term to measure relevance to

propertyToSortBy Func<T, string>

x => x.PropertyName [the term will be matched against the value of this property]

maxDistance int?

The maximum levenstein distance to qualify an item for inclusion in the returned list

Returns

IEnumerable<T>

Type Parameters

T

Any object type

ToBatches<T>(IEnumerable<T>, int)

Extension method for processing collections in batches with streaming (yield return)

public static IEnumerable<IEnumerable<T>> ToBatches<T>(this IEnumerable<T> collection, int batchSize = 100)

Parameters

collection IEnumerable<T>

The source collection

batchSize int

The size of each batch

Returns

IEnumerable<IEnumerable<T>>

Type Parameters

T

The type of the objects inside the source collection

ToDate(DateTime)

converts a DateTime instance to a Date instance.

public static Date ToDate(this DateTime dateTime)

Parameters

dateTime DateTime

the DateTime instance to convert

Returns

Date

ToDate(long)

converts ticks to a Date instance.

public static Date ToDate(this long ticks)

Parameters

ticks long

the ticks to convert

Returns

Date

ToDocument<T>(T)

Creates an unlinked duplicate of the original IEntity ready for embedding with a blank ID.

public static T ToDocument<T>(this T entity) where T : IEntity

Parameters

entity T

Returns

T

Type Parameters

T

ToDocuments<T>(IEnumerable<T>, DB?)

Creates unlinked duplicates of the original Entities ready for embedding with blank IDs.

public static IEnumerable<T> ToDocuments<T>(this IEnumerable<T> entities, DB? dbInstance = null) where T : IEntity

Parameters

entities IEnumerable<T>
dbInstance DB

Returns

IEnumerable<T>

Type Parameters

T

ToDocuments<T>(T[], DB?)

Creates unlinked duplicates of the original Entities ready for embedding with blank IDs.

public static T[] ToDocuments<T>(this T[] entities, DB? dbInstance = null) where T : IEntity

Parameters

entities T[]
dbInstance DB

Returns

T[]

Type Parameters

T

ToDoubleMetaphoneHash(string)

Converts a search term to Double Metaphone hash code suitable for fuzzy text searching.

public static string ToDoubleMetaphoneHash(this string term)

Parameters

term string

A single or multiple word search term

Returns

string

ToFuzzy(string)

converts a string value to a FuzzyString

public static FuzzyString ToFuzzy(this string value)

Parameters

value string

the string to convert

Returns

FuzzyString

ToReference<T>(T)

Returns a reference to this entity.

public static One<T> ToReference<T>(this T entity) where T : IEntity

Parameters

entity T

Returns

One<T>

Type Parameters

T