---
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: "nodejs"
---

# Changes API: How to Subscribe to Document Changes

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/changes/how-to-subscribe-to-document-changes.md), [Java](https://docs.ravendb.net/6.2/client-api/changes/how-to-subscribe-to-document-changes.java.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."

Following methods allow you to subscribe to document changes:

- [forDocument()](#fordocument)
- [forDocumentsInCollection()](#fordocumentsincollection)
- [forDocumentsStartingWith()](#fordocumentsstartingwith)
- [forAllDocuments()](#foralldocuments)

## forDocument

Single document changes can be observed using `forDocument()` method.

### Syntax

```javascript
store.changes().forDocument(docId);
```

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

| Return Value | |
| - | - |
| IChangesObservable<[DocumentChange](#documentchange)> | Observable that allows to add listeners for events for given document. |

### Example

```javascript
store.changes().forDocument("employees/1")
  .on("error", err => {
      //handle error 
  })
  .on("data", change => {
      switch (change.type) {
          case "Put":
              // do something
              break;
          case "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

```javascript
store.changes().forDocumentsInCollection(collectionName);
store.changes().forDocumentsInCollection(clazz);
```

| Parameters | | |
| - | - | - |
| **collectionName** | string | Name of document collection for which notifications will be processed. |

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

> **Info**
>
> Overload with entity type uses `conventions.getCollectionNameForType()` to get collection name.

### Example

```javascript
store.changes().forDocumentsInCollection(Employee)
  .on("data", change => {
      console.log(change.type + " on document " + change.id);
  });
```

or

```javascript
const collectionName = store.conventions.getCollectionNameForType(Employee);
store.changes()
  .forDocumentsInCollection(collectionName)
  .on("data", change => {
      console.log(change.type + " on document " + change.id);
  });
```

## forDocumentsStartingWith

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

### Syntax

```javascript
store.changes().forDocumentsStartingWith(docIdPrefix);
```

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

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

### Example

```javascript
store.changes()
  .forDocumentsStartingWith("employees/1") // employees/1, employees/10, employees/11, etc.
  .on("data", change => {
      console.log(change.type + " on document " + change.id);
  });
```

## forAllDocuments

To observe all document changes use `forAllDocuments()` method.

### Syntax

```javascript
store.changes().forAllDocuments();
```

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

### Example

```javascript
store.changes().forAllDocuments()
  .on("data", change => {
      console.log(change.type + " on document " + change.id);
  });
```

## DocumentChange

| Name | Type | Description |
| - | - | - |
| **type** | [DocumentChangeTypes](#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 |
| - |
| **None** |
| **Put** |
| **Delete** |
| **BulkInsertStarted** |
| **BulkInsertEnded** |
| **BulkInsertError** |
| **DeleteOnTombstoneReplication** |
| **Conflict** |
| **Common** |

## See also

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