Changes API: How to Subscribe to Document Changes

Following methods allow you to subscribe to document changes:

ForDocument

Single document changes can be observed using ForDocument method.

Syntax

IChangesObservable<DocumentChange> ForDocument(string docId);
Parameters
docId string ID of a document for which notifications will be processed.
Return Value
IChangesObservable<DocumentChange> Observable that allows to add subscriptions to notifications for given document.

Example

IDisposable subscription = store
    .Changes()
    .ForDocument("employees/1")
    .Subscribe(
        change =>
        {
            switch (change.Type)
            {
                case DocumentChangeTypes.Put:
                    // do something
                    break;
                case DocumentChangeTypes.Delete:
                    // do something
                    break;
            }
        });

ForDocumentsInCollection

To observe all document changes in particular collection use ForDocumentInCollection method. This method filters documents by @collection metadata property value.

Syntax

IChangesObservable<DocumentChange> ForDocumentsInCollection(string collectionName);

IChangesObservable<DocumentChange> ForDocumentsInCollection<TEntity>();
Parameters
collectionName string Name of document collection for which notifications will be processed.
Return Value
IChangesObservable<DocumentChange> Observable that allows to add subscriptions to notifications for given document collection name.

Information

Overload with TEntity type uses Conventions.GetCollectionName to get collection name.

Example

IDisposable subscription = store
    .Changes()
    .ForDocumentsInCollection<Employee>()
    .Subscribe(change => Console.WriteLine("{0} on document {1}", change.Type, change.Id));

or

string collectionName = store.Conventions.FindCollectionName(typeof(Employee));
IDisposable subscription = store
    .Changes()
    .ForDocumentsInCollection(collectionName)
    .Subscribe(change => Console.WriteLine("{0} on document {1}", change.Type, change.Id));

ForDocumentsStartingWith

To observe all document changes for documents with ID that contains given prefix use ForDocumentsStartingWith method.

Syntax

IChangesObservable<DocumentChange> ForDocumentsStartingWith(string docIdPrefix);
Parameters
docIdPrefix string Document ID prefix for which notifications will be processed.
Return Value
IChangesObservable<DocumentChange> Observable that allows to add subscriptions to notifications for given document ID prefix.

Example

IDisposable subscription = store
    .Changes()
    .ForDocumentsStartingWith("employees/1") // employees/1, employees/10, employees/11, etc.
    .Subscribe(change => Console.WriteLine("{0} on document {1}", change.Type, change.Id));

ForAllDocuments

To observe all document changes use ForAllDocuments method.

Syntax

IChangesObservable<DocumentChange> ForAllDocuments();
Return Value
IChangesObservable<DocumentChange> Observable that allows to add subscriptions to notifications for all documents.

Example

IDisposable subscription = store
    .Changes()
    .ForAllDocuments() // employees/1, orders/1, customers/1, etc.
    .Subscribe(change => Console.WriteLine("{0} on document {1}", change.Type, change.Id));

DocumentChange

Name Type Description
Type DocumentChangeTypes Document change type enum
Id string Document identifier
CollectionName string Document's collection name
TypeName string Type name
ChangeVector string Document's ChangeVector

DocumentChangeTypes

Name Value
None 0
Put 1
Delete 2
BulkInsertStarted 4
BulkInsertEnded 8
BulkInsertError 16
DeleteOnTombstoneReplication 32
Conflict 64
Common Put & Delete

Remarks

To get more method overloads, especially ones supporting delegates, please add the System.Reactive.Core package to your project.