Class MongoCollectionExtensions

Namespace
MongoDB.Driver
Assembly
Cratis.Arc.MongoDB.dll

Extension methods for MongoDB.Driver.IMongoCollection<TDocument>.

public static class MongoCollectionExtensions
Inheritance
MongoCollectionExtensions
Inherited Members

Methods

FindByIdAsync<T, TId>(IMongoCollection<T>, TId)

Find a single document based on Id - asynchronous.

public static Task<T?> FindByIdAsync<T, TId>(this IMongoCollection<T> collection, TId id)

Parameters

collection IMongoCollection<T>

MongoDB.Driver.IMongoCollection<TDocument> to extend.

id TId

Id of document.

Returns

Task<T>

The document if found, default if not.

Type Parameters

T

Type of document.

TId

Type of identifier.

FindById<T, TId>(IMongoCollection<T>, TId)

Find a single document based on Id.

public static T? FindById<T, TId>(this IMongoCollection<T> collection, TId id)

Parameters

collection IMongoCollection<T>

MongoDB.Driver.IMongoCollection<TDocument> to extend.

id TId

Id of document.

Returns

T

The document if found, default if not.

Type Parameters

T

Type of document.

TId

Type of identifier.

ObserveById<TDocument, TId>(IMongoCollection<TDocument>, TId)

Observe a document by id without query-context paging, sorting, or counting.

public static ISubject<TDocument> ObserveById<TDocument, TId>(this IMongoCollection<TDocument> collection, TId id)

Parameters

collection IMongoCollection<TDocument>

The collection to observe.

id TId

The document id.

Returns

ISubject<TDocument>

The observable document.

Type Parameters

TDocument

The document type.

TId

The id type.

ObserveById<TDocument, TId>(IMongoCollection<TDocument>, TId, bool)

Create an observable query that will observe a single document based on Id of the document in the collection for changes matching the filter criteria.

public static ISubject<TDocument> ObserveById<TDocument, TId>(this IMongoCollection<TDocument> collection, TId id, bool ignoreQueryContext)

Parameters

collection IMongoCollection<TDocument>

MongoDB.Driver.IMongoCollection<TDocument> to extend.

id TId

The identifier of the document to observe.

ignoreQueryContext bool

Retained for compatibility; has no effect for single-result observations, which always read the full filtered source without client paging, sorting, or total count updates.

Returns

ISubject<TDocument>

An System.Reactive.Subjects.ISubject<T> with an instance of the type; emits default when no document matches — after the observed document is deleted, or when the initial query finds none.

Type Parameters

TDocument

Type of document in the collection.

TId

Type of id - key.

ObserveSingle<TDocument>(IMongoCollection<TDocument>, FilterDefinition<TDocument>?, FindOptions?)

Observe a single matching document without query-context paging, sorting, or counting.

public static ISubject<TDocument> ObserveSingle<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument>? filter = null, FindOptions? options = null)

Parameters

collection IMongoCollection<TDocument>

The collection to observe.

filter FilterDefinition<TDocument>

Optional filter.

options FindOptions

Optional find options.

Returns

ISubject<TDocument>

The observable document.

Type Parameters

TDocument

The document type.

ObserveSingle<TDocument>(IMongoCollection<TDocument>, FilterDefinition<TDocument>?, bool, FindOptions?)

Create an observable query that will observe the collection for changes matching the filter criteria.

public static ISubject<TDocument> ObserveSingle<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument>? filterDefinition, bool ignoreQueryContext, FindOptions? options = null)

Parameters

collection IMongoCollection<TDocument>

MongoDB.Driver.IMongoCollection<TDocument> to extend.

filterDefinition FilterDefinition<TDocument>

The MongoDB filter.

ignoreQueryContext bool

Retained for compatibility; has no effect for single-result observations, which always read the full filtered source without client paging, sorting, or total count updates.

options FindOptions

Optional options.

Returns

ISubject<TDocument>

An System.Reactive.Subjects.ISubject<T> with a single instance of the type; emits default when no document matches — after the observed document is deleted, after an update or replace moves it out of the filter, or when the initial query finds none.

Type Parameters

TDocument

Type of document in the collection.

Examples

collection.ObserveSingle(filterDefinition, ignoreQueryContext: true).

ObserveSingle<TDocument>(IMongoCollection<TDocument>, bool, FilterDefinition<TDocument>?, FindOptions?)

Observe a single matching document with the compatibility query-context option before the MongoDB filter.

public static ISubject<TDocument> ObserveSingle<TDocument>(this IMongoCollection<TDocument> collection, bool ignoreQueryContext, FilterDefinition<TDocument>? filter = null, FindOptions? options = null)

Parameters

collection IMongoCollection<TDocument>

MongoDB.Driver.IMongoCollection<TDocument> to extend.

ignoreQueryContext bool

Retained for compatibility; has no effect for single-result observations, which always read the full filtered source without client paging, sorting, or total count updates.

filter FilterDefinition<TDocument>

Optional filter.

options FindOptions

Optional options.

Returns

ISubject<TDocument>

An System.Reactive.Subjects.ISubject<T> with a single instance of the type; emits default when no document matches — after the observed document is deleted, after an update or replace moves it out of the filter, or when the initial query finds none.

Type Parameters

TDocument

Type of document in the collection.

ObserveSingle<TDocument>(IMongoCollection<TDocument>, Expression<Func<TDocument, bool>>?, FindOptions?)

Observe a single matching document without query-context paging, sorting, or counting.

public static ISubject<TDocument> ObserveSingle<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>>? filter, FindOptions? options = null)

Parameters

collection IMongoCollection<TDocument>

The collection to observe.

filter Expression<Func<TDocument, bool>>

Optional filter.

options FindOptions

Optional find options.

Returns

ISubject<TDocument>

The observable document.

Type Parameters

TDocument

The document type.

ObserveSingle<TDocument>(IMongoCollection<TDocument>, Expression<Func<TDocument, bool>>?, bool, FindOptions?)

Create an observable query that will observe the collection for changes matching the filter criteria.

public static ISubject<TDocument> ObserveSingle<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>>? filter, bool ignoreQueryContext, FindOptions? options = null)

Parameters

collection IMongoCollection<TDocument>

MongoDB.Driver.IMongoCollection<TDocument> to extend.

filter Expression<Func<TDocument, bool>>

Optional filter.

ignoreQueryContext bool

Retained for compatibility; has no effect for single-result observations, which always read the full filtered source without client paging, sorting, or total count updates.

options FindOptions

Optional options.

Returns

ISubject<TDocument>

An System.Reactive.Subjects.ISubject<T> with a single instance of the type; emits default when no document matches — after the observed document is deleted, after an update or replace moves it out of the filter, or when the initial query finds none.

Type Parameters

TDocument

Type of document in the collection.

Observe<TDocument>(IMongoCollection<TDocument>, FilterDefinition<TDocument>?, FindOptions?)

Observe matching documents using the current query context.

public static ISubject<IEnumerable<TDocument>> Observe<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument>? filter = null, FindOptions? options = null)

Parameters

collection IMongoCollection<TDocument>

The collection to observe.

filter FilterDefinition<TDocument>

Optional filter.

options FindOptions

Optional find options.

Returns

ISubject<IEnumerable<TDocument>>

The observable documents.

Type Parameters

TDocument

The document type.

Observe<TDocument>(IMongoCollection<TDocument>, FilterDefinition<TDocument>?, bool, FindOptions?)

Create an observable query that will observe the collection for changes matching the filter criteria.

public static ISubject<IEnumerable<TDocument>> Observe<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument>? filterDefinition, bool ignoreQueryContext, FindOptions? options = null)

Parameters

collection IMongoCollection<TDocument>

MongoDB.Driver.IMongoCollection<TDocument> to extend.

filterDefinition FilterDefinition<TDocument>

The MongoDB filter.

ignoreQueryContext bool

Whether to read the full filtered source without client paging, sorting, or total count updates.

options FindOptions

Optional options.

Returns

ISubject<IEnumerable<TDocument>>

System.Reactive.Subjects.ISubject<T> with a collection of the type for the collection.

Type Parameters

TDocument

Type of document in the collection.

Examples

collection.Observe(filterDefinition, ignoreQueryContext: true).

Observe<TDocument>(IMongoCollection<TDocument>, bool, FilterDefinition<TDocument>?, FindOptions?)

Observe matching documents with the query-context option before the MongoDB filter.

public static ISubject<IEnumerable<TDocument>> Observe<TDocument>(this IMongoCollection<TDocument> collection, bool ignoreQueryContext, FilterDefinition<TDocument>? filter = null, FindOptions? options = null)

Parameters

collection IMongoCollection<TDocument>

MongoDB.Driver.IMongoCollection<TDocument> to extend.

ignoreQueryContext bool

Whether to ignore client paging, sorting, and total count updates.

filter FilterDefinition<TDocument>

Optional filter.

options FindOptions

Optional options.

Returns

ISubject<IEnumerable<TDocument>>

System.Reactive.Subjects.ISubject<T> with a collection of the type for the collection.

Type Parameters

TDocument

Type of document in the collection.

Observe<TDocument>(IMongoCollection<TDocument>, Expression<Func<TDocument, bool>>?, FindOptions?)

Observe matching documents using the current query context.

public static ISubject<IEnumerable<TDocument>> Observe<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>>? filter, FindOptions? options = null)

Parameters

collection IMongoCollection<TDocument>

The collection to observe.

filter Expression<Func<TDocument, bool>>

Optional filter.

options FindOptions

Optional find options.

Returns

ISubject<IEnumerable<TDocument>>

The observable documents.

Type Parameters

TDocument

The document type.

Observe<TDocument>(IMongoCollection<TDocument>, Expression<Func<TDocument, bool>>?, bool, FindOptions?)

Create an observable query that will observe the collection for changes matching the filter criteria.

public static ISubject<IEnumerable<TDocument>> Observe<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>>? filter, bool ignoreQueryContext, FindOptions? options = null)

Parameters

collection IMongoCollection<TDocument>

MongoDB.Driver.IMongoCollection<TDocument> to extend.

filter Expression<Func<TDocument, bool>>

Optional filter.

ignoreQueryContext bool

Whether to read the full filtered source without client paging, sorting, or total count updates.

options FindOptions

Optional options.

Returns

ISubject<IEnumerable<TDocument>>

System.Reactive.Subjects.ISubject<T> with a collection of the type for the collection.

Type Parameters

TDocument

Type of document in the collection.