Table of Contents

Class Watcher<T>

Namespace
MongoDB.Entities
Assembly
MongoDB.Entities.dll

Watcher for subscribing to mongodb change streams.

public class Watcher<T> where T : IEntity

Type Parameters

T

The type of entity

Inheritance
Watcher<T>
Inherited Members

Properties

CanRestart

Returns true if watching can be restarted if it was stopped due to an error or invalidate event. Will always return false after cancellation is requested via the cancellation token.

public bool CanRestart { get; }

Property Value

bool

IsInitialized

Indicates whether this watcher has already been initialized or not.

public bool IsInitialized { get; }

Property Value

bool

Name

The name of this watcher instance

public string Name { get; }

Property Value

string

ResumeToken

The last resume token received from mongodb server. Can be used to resume watching with .StartWithToken() method.

public BsonDocument? ResumeToken { get; }

Property Value

BsonDocument

Methods

ReStart(BsonDocument?)

If the watcher stopped due to an error or invalidate event, you can try to restart the watching again with this method.

public void ReStart(BsonDocument? resumeToken = null)

Parameters

resumeToken BsonDocument

An optional resume token to restart watching with

Start(EventType, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>, int, bool, bool, CancellationToken)

Starts the watcher instance with the supplied parameters

public void Start(EventType eventTypes, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>> filter, int batchSize = 25, bool onlyGetIDs = false, bool autoResume = true, CancellationToken cancellation = default)

Parameters

eventTypes EventType

Type of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted

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

b => b.Eq(d => d.FullDocument.Prop1, "value")

batchSize int

The max number of entities to receive for a single event occurence

onlyGetIDs bool

Set to true if you don't want the complete entity details. All properties except the ID will then be null.

autoResume bool

Set to false if you'd like to skip the changes that happened while the watching was stopped. This will also make you unable to retrieve a ResumeToken.

cancellation CancellationToken

A cancellation token for ending the watching/change stream

Start(EventType, Expression<Func<ChangeStreamDocument<T>, bool>>?, int, bool, bool, CancellationToken)

Starts the watcher instance with the supplied parameters

public void Start(EventType eventTypes, Expression<Func<ChangeStreamDocument<T>, bool>>? filter = null, int batchSize = 25, bool onlyGetIDs = false, bool autoResume = true, CancellationToken cancellation = default)

Parameters

eventTypes EventType

Type of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted

filter Expression<Func<ChangeStreamDocument<T>, bool>>

x => x.FullDocument.Prop1 == "SomeValue"

batchSize int

The max number of entities to receive for a single event occurence

onlyGetIDs bool

Set to true if you don't want the complete entity details. All properties except the ID will then be null.

autoResume bool

Set to false if you'd like to skip the changes that happened while the watching was stopped. This will also make you unable to retrieve a ResumeToken.

cancellation CancellationToken

A cancellation token for ending the watching/change stream

Start(EventType, Expression<Func<T, T>>, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>, int, bool, CancellationToken)

Starts the watcher instance with the supplied parameters. Supports projection.

public void Start(EventType eventTypes, Expression<Func<T, T>> projection, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>> filter, int batchSize = 25, bool autoResume = true, CancellationToken cancellation = default)

Parameters

eventTypes EventType

Type of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted

projection Expression<Func<T, T>>

A projection expression for the entity

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

b => b.Eq(d => d.FullDocument.Prop1, "value")

batchSize int

The max number of entities to receive for a single event occurence

autoResume bool

Set to false if you'd like to skip the changes that happened while the watching was stopped. This will also make you unable to retrieve a ResumeToken.

cancellation CancellationToken

A cancellation token for ending the watching/change stream

Start(EventType, Expression<Func<T, T>>, Expression<Func<ChangeStreamDocument<T>, bool>>?, int, bool, CancellationToken)

Starts the watcher instance with the supplied parameters. Supports projection.

public void Start(EventType eventTypes, Expression<Func<T, T>> projection, Expression<Func<ChangeStreamDocument<T>, bool>>? filter = null, int batchSize = 25, bool autoResume = true, CancellationToken cancellation = default)

Parameters

eventTypes EventType

Type of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted

projection Expression<Func<T, T>>

A projection expression for the entity

filter Expression<Func<ChangeStreamDocument<T>, bool>>

x => x.FullDocument.Prop1 == "SomeValue"

batchSize int

The max number of entities to receive for a single event occurence

autoResume bool

Set to false if you'd like to skip the changes that happened while the watching was stopped. This will also make you unable to retrieve a ResumeToken.

cancellation CancellationToken

A cancellation token for ending the watching/change stream

StartWithToken(BsonDocument, EventType, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>, int, bool, CancellationToken)

Starts the watcher instance with the supplied configuration

public void StartWithToken(BsonDocument resumeToken, EventType eventTypes, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>> filter, int batchSize = 25, bool onlyGetIDs = false, CancellationToken cancellation = default)

Parameters

resumeToken BsonDocument

A resume token to start receiving changes after some point back in time

eventTypes EventType

Type of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted

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

b => b.Eq(d => d.FullDocument.Prop1, "value")

batchSize int

The max number of entities to receive for a single event occurence

onlyGetIDs bool

Set to true if you don't want the complete entity details. All properties except the ID will then be null.

cancellation CancellationToken

A cancellation token for ending the watching/change stream

StartWithToken(BsonDocument, EventType, Expression<Func<ChangeStreamDocument<T>, bool>>?, int, bool, CancellationToken)

Starts the watcher instance with the supplied configuration

public void StartWithToken(BsonDocument resumeToken, EventType eventTypes, Expression<Func<ChangeStreamDocument<T>, bool>>? filter = null, int batchSize = 25, bool onlyGetIDs = false, CancellationToken cancellation = default)

Parameters

resumeToken BsonDocument

A resume token to start receiving changes after some point back in time

eventTypes EventType

Type of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted

filter Expression<Func<ChangeStreamDocument<T>, bool>>

x => x.FullDocument.Prop1 == "SomeValue"

batchSize int

The max number of entities to receive for a single event occurence

onlyGetIDs bool

Set to true if you don't want the complete entity details. All properties except the ID will then be null.

cancellation CancellationToken

A cancellation token for ending the watching/change stream

StartWithToken(BsonDocument, EventType, Expression<Func<T, T>>, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>, int, CancellationToken)

Starts the watcher instance with the supplied configuration. Supports projection.

public void StartWithToken(BsonDocument resumeToken, EventType eventTypes, Expression<Func<T, T>> projection, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>> filter, int batchSize = 25, CancellationToken cancellation = default)

Parameters

resumeToken BsonDocument

A resume token to start receiving changes after some point back in time

eventTypes EventType

Type of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted

projection Expression<Func<T, T>>

A projection expression for the entity

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

b => b.Eq(d => d.FullDocument.Prop1, "value")

batchSize int

The max number of entities to receive for a single event occurence

cancellation CancellationToken

A cancellation token for ending the watching/change stream

StartWithToken(BsonDocument, EventType, Expression<Func<T, T>>, Expression<Func<ChangeStreamDocument<T>, bool>>?, int, CancellationToken)

Starts the watcher instance with the supplied configuration. Supports projection.

public void StartWithToken(BsonDocument resumeToken, EventType eventTypes, Expression<Func<T, T>> projection, Expression<Func<ChangeStreamDocument<T>, bool>>? filter = null, int batchSize = 25, CancellationToken cancellation = default)

Parameters

resumeToken BsonDocument

A resume token to start receiving changes after some point back in time

eventTypes EventType

Type of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted

projection Expression<Func<T, T>>

A projection expression for the entity

filter Expression<Func<ChangeStreamDocument<T>, bool>>

x => x.FullDocument.Prop1 == "SomeValue"

batchSize int

The max number of entities to receive for a single event occurence

cancellation CancellationToken

A cancellation token for ending the watching/change stream

Events

OnChanges

This event is fired when the desired types of events have occured. Will have a list of 'entities' that was received as input.

public event Action<IEnumerable<T>>? OnChanges

Event Type

Action<IEnumerable<T>>

OnChangesAsync

This event is fired when the desired types of events have occured. Will have a list of 'entities' that was received as input.

public event AsyncEventHandler<IEnumerable<T>>? OnChangesAsync

Event Type

AsyncEventHandler<IEnumerable<T>>

OnChangesCSD

This event is fired when the desired types of events have occured. Will have a list of 'ChangeStreamDocuments' that was received as input.

public event Action<IEnumerable<ChangeStreamDocument<T>>>? OnChangesCSD

Event Type

Action<IEnumerable<ChangeStreamDocument<T>>>

OnChangesCSDAsync

This event is fired when the desired types of events have occured. Will have a list of 'ChangeStreamDocuments' that was received as input.

public event AsyncEventHandler<IEnumerable<ChangeStreamDocument<T>>>? OnChangesCSDAsync

Event Type

AsyncEventHandler<IEnumerable<ChangeStreamDocument<T>>>

OnError

This event is fired when an exception is thrown in the change-stream.

public event Action<Exception>? OnError

Event Type

Action<Exception>

OnStop

This event is fired when the internal cursor get closed due to an 'invalidate' event or cancellation is requested via the cancellation token.

public event Action? OnStop

Event Type

Action