---
title: "Changes API: How to Subscribe to Document Changes"
url: "https://docs.ravendb.net/6.2/client-api/changes/how-to-subscribe-to-document-changes"
version: "6.2"
language: "csharp"
---

# Changes API: How to Subscribe to Document Changes

> Client language: C#. Also available for [Java](https://docs.ravendb.net/6.2/client-api/changes/how-to-subscribe-to-document-changes.java.md), [Node.js](https://docs.ravendb.net/6.2/client-api/changes/how-to-subscribe-to-document-changes.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> Subscribe to document changes using `ForDocument`, `ForDocumentsInCollection`,
> `ForDocumentsStartingWith`, or `ForAllDocuments`.
>
> > **Warning**
> >
> > The `Subscribe` method on the observable these methods return accepts only an `IObserver` object
> > by default.  
> > To instead subscribe with a delegate, like the `change => ...` lambda used in the examples below,
> > add the [System.Reactive](https://www.nuget.org/packages/System.Reactive/) package to your project.  
> > This adds `Subscribe` overloads that take delegates, including ones with error and
> > completion handlers.
>
> - In this article:
>   - [ForDocument](#fordocument)
>   - [ForDocumentsInCollection](#fordocumentsincollection)
>   - [ForDocumentsStartingWith](#fordocumentsstartingwith)
>   - [ForAllDocuments](#foralldocuments)
>   - [DocumentChange and DocumentChangeTypes syntax](#documentchange-and-documentchangetypes-syntax)

## ForDocument {#fordocument}

Single document changes can be observed using `ForDocument` method.

### Syntax

```csharp
IChangesObservable<DocumentChange> ForDocument(string docId);
```

| Parameters | | |
| - | - | - |
| **docId** | string | ID of a document for which notifications will be processed. |

| Return Value | |
| - | - |
| IChangesObservable<[DocumentChange](#documentchange-and-documentchangetypes-syntax)> | Observable that allows to add subscriptions to notifications for given document. |

### Example

```csharp
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 {#fordocumentsincollection}

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

### Syntax

```csharp
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](#documentchange-and-documentchangetypes-syntax)> | Observable that allows to add subscriptions to notifications for given document collection name. |

> **Info**
>
> Overload with `TEntity` type uses `Conventions.GetCollectionName` to get collection name.

### Example

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

or

```csharp
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 {#fordocumentsstartingwith}

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

### Syntax

```csharp
IChangesObservable<DocumentChange> ForDocumentsStartingWith(string docIdPrefix);
```

| Parameters | | |
| - | - | - |
| **docIdPrefix** | string | Document ID prefix for which notifications will be processed. |

| Return Value | |
| - | - |
| IChangesObservable<[DocumentChange](#documentchange-and-documentchangetypes-syntax)> | Observable that allows to add subscriptions to notifications for given document ID prefix. |

### Example

```csharp
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 {#foralldocuments}

To observe all document changes use `ForAllDocuments` method.

### Syntax

```csharp
IChangesObservable<DocumentChange> ForAllDocuments();
```

| Return Value | |
| - | - |
| IChangesObservable<[DocumentChange](#documentchange-and-documentchangetypes-syntax)> | Observable that allows to add subscriptions to notifications for all documents. |

### Example

```csharp
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 and DocumentChangeTypes syntax {#documentchange-and-documentchangetypes-syntax}

### DocumentChange

**The notification object delivered to subscribers when a document changes.**

```csharp
class DocumentChange
{
    DocumentChangeTypes Type
    string Id
    string CollectionName
    string ChangeVector
}
```

| Property | Type | Description |
| - | - | - |
| **Type** | [DocumentChangeTypes](#documentchange-and-documentchangetypes-syntax) | Document change type enum |
| **Id** | string | Document identifier |
| **CollectionName** | string | Document's collection name |
| **ChangeVector** | string | Document's ChangeVector |

### DocumentChangeTypes

```csharp
enum DocumentChangeTypes
{
    None,
    Put,
    Delete,
    BulkInsertStarted,
    BulkInsertEnded,
    BulkInsertError,
    DeleteOnTombstoneReplication,
    Conflict,
    Common,
}
```

| Name | Value |
| - | - |
| **None** | `0` |
| **Put** | `1` |
| **Delete** | `2` |
| **BulkInsertStarted** | `4` |
| **BulkInsertEnded** | `8` |
| **BulkInsertError** | `16` |
| **DeleteOnTombstoneReplication** | `32` |
| **Conflict** | `64` |
| **Common** | `Put \| Delete` |

## See also

- [What is Changes API](https://docs.ravendb.net/6.2/client-api/changes/what-is-changes-api.md)
- [How to Subscribe to Index Changes](https://docs.ravendb.net/6.2/client-api/changes/how-to-subscribe-to-index-changes.md)
- [How to Subscribe to Operation Changes](https://docs.ravendb.net/6.2/client-api/changes/how-to-subscribe-to-operation-changes.md)
