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

# Changes API: How to Subscribe to Document Changes

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

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

```java
IChangesObservable<DocumentChange> forDocument(String docId);
```

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

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

### Example

```java
CleanCloseable subscription = store.changes()
  .forDocument("employees/1")
  .subscribe(Observers.create(change -> {
      switch (change.getType()) {
          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

```java
IChangesObservable<DocumentChange> forDocumentsInCollection(String collectionName);

IChangesObservable<DocumentChange> forDocumentsInCollection(Class<?> 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 `TEntity` type uses `conventions.GetCollectionName` to get collection name.

### Example

```java
CleanCloseable subscription = store
  .changes()
  .forDocumentsInCollection(Employee.class)
  .subscribe(Observers.create(change -> {
      System.out.println(change.getType() + " on document " + change.getId());
  }));
```

or

```java
String collectionName = store.getConventions().getFindCollectionName().apply(Employee.class);
CleanCloseable subscription = store
  .changes()
  .forDocumentsInCollection(collectionName)
  .subscribe(Observers.create(change -> {
      System.out.println(change.getType() + " on document " + change.getId());
  }));
```

## forDocumentsStartingWith

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

### Syntax

```java
IChangesObservable<DocumentChange> forDocumentsStartingWith(String 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

```java
CleanCloseable subscription = store
  .changes()
  .forDocumentsStartingWith("employees/1") // employees/1, employees/10, employees/11, etc.
  .subscribe(Observers.create(change -> {
      System.out.println(change.getType() + " on document " + change.getId());
  }));
```

## forAllDocuments

To observe all document changes use `forAllDocuments` method.

### Syntax

```java
IChangesObservable<DocumentChange> forAllDocuments();
```

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

### Example

```java
CleanCloseable subscription = store
  .changes()
  .forAllDocuments()
  .subscribe(Observers.create(change -> {
      System.out.println(change.getType() + " on document " + change.getId());
  }));
```

## 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** |
| **BULK\_INSERT\_STARTED** |
| **BULK\_INSERT\_ENDED** |
| **BULK\_INSERT\_ERROR** |
| **DELETE\_ON\_TOMBSTONE\_REPLICATION** |
| **CONFLICT** |
| **COMMON** |

## See also

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