DEV Community

Cover image for Understanding Change Streams in MongoDB
Guilherme Nichetti
Guilherme Nichetti

Posted on

Understanding Change Streams in MongoDB

In the dynamic landscape of modern applications, real-time data processing has become essential. MongoDB, a popular NoSQL database, offers a powerful feature called Change Streams, enabling applications to track changes in the database in real-time. This article explores what Change Streams are, their benefits, and how to implement them in MongoDB using Java.

What are Change Streams?

Change Streams in MongoDB allow applications to listen to real-time data changes on a collection, database, or deployment level. This feature leverages MongoDB’s replication capabilities to provide a continuous, reliable stream of data changes, making it ideal for building reactive applications, audit trails, real-time analytics, and more.

Benefits of Using Change Streams

  1. Real-Time Data Processing: Applications can respond instantly to data changes, ensuring timely updates and actions.

  2. Scalability: Change Streams can be used across distributed systems, handling large volumes of data efficiently.

  3. Simplicity: Implementing Change Streams is straightforward, reducing the complexity of managing custom polling mechanisms.

  4. Granularity: They offer fine-grained control, allowing you to listen to changes at various levels – from a single collection to an entire deployment.

How Change Streams Work?

Change Streams work by leveraging MongoDB’s oplog (operations log) in replica sets. When an operation (insert, update, delete, etc.) is performed on the database, it is recorded in the oplog. Change Streams tap into this oplog to emit events corresponding to these operations.

Implementing Change Streams

1. Setup MongoDB Replica Set

• Ensure your MongoDB instance is running as a replica set. If you're using a standalone MongoDB instance, convert it to a replica set:

mongod --replSet rs0
Enter fullscreen mode Exit fullscreen mode

• Initialize the replica set:

rs.initiate()
Enter fullscreen mode Exit fullscreen mode

2. Connecting to MongoDB and Watching a Change Stream

• Add MongoDB Java Driver Dependency:
If you are using Maven, add the following dependency to your pom.xml:

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-sync</artifactId>
    <version>4.4.0</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

• Watch Change Streams on a Collection:

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.changestream.ChangeStreamDocument;
import com.mongodb.client.model.changestream.OperationType;
import org.bson.Document;

public class ChangeStreamExample {
    public static void main(String[] args) {
        String uri = "mongodb://localhost:27017";
        try (MongoClient mongoClient = MongoClients.create(uri)) {
            MongoDatabase database = mongoClient.getDatabase("exampleDB");
            MongoCollection<Document> collection = database.getCollection("exampleCollection");

            collection.watch().forEach((ChangeStreamDocument<Document> change) -> {
                System.out.println("Change detected: " + change);
                if (change.getOperationType() == OperationType.INSERT) {
                    System.out.println("Document inserted: " + change.getFullDocument());
                } else if (change.getOperationType() == OperationType.UPDATE) {
                    System.out.println("Document updated: " + change.getUpdateDescription());
                } else if (change.getOperationType() == OperationType.DELETE) {
                    System.out.println("Document deleted: " + change.getDocumentKey());
                }
            });

            System.out.println("Watching for changes...");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Handling Different Change Events
• Change Streams can capture various types of changes, including inserts, updates, deletes, and replacements. Here’s how to handle different event types:

collection.watch().forEach((ChangeStreamDocument<Document> change) -> {
    switch (change.getOperationType()) {
        case INSERT:
            System.out.println("Document inserted: " + change.getFullDocument());
            break;
        case UPDATE:
            System.out.println("Document updated: " + change.getUpdateDescription());
            break;
        case DELETE:
            System.out.println("Document deleted: " + change.getDocumentKey());
            break;
        default:
            System.out.println("Other operation: " + change);
    }
});
Enter fullscreen mode Exit fullscreen mode

4. Filtering Change Streams
• You can filter Change Streams to only listen to specific changes using aggregation pipelines.

import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;
import org.bson.conversions.Bson;

import java.util.Arrays;
import java.util.List;

List<Bson> pipeline = Arrays.asList(
    Aggregates.match(Filters.eq("fullDocument.status", "active"))
);

collection.watch(pipeline).forEach((ChangeStreamDocument<Document> change) -> {
    System.out.println("Filtered change detected: " + change);
});
Enter fullscreen mode Exit fullscreen mode

5. Watching Changes at the Database Level (To watch changes across all collections in a database)

database.watch().forEach((ChangeStreamDocument<Document> change) -> {
    System.out.println("Change detected in database: " + change);
});
Enter fullscreen mode Exit fullscreen mode

6. Watching Changes at the Database Level (To watch changes across the entire deployment)

mongoClient.watch().forEach((ChangeStreamDocument<Document> change) -> {
    System.out.println("Change detected in deployment: " + change);
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

• MongoDB Change Streams provide a robust mechanism for building real-time, reactive applications. By leveraging this feature, developers can efficiently track and respond to data changes, enabling a wide range of use cases from live notifications to real-time analytics. With the simplicity and power of Change Streams, MongoDB continues to be a strong contender in the world of modern databases.

Top comments (0)