codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

53 / 65 topics
52Real-Time Applications53Change Streams54Full-Text Search55Geospatial Data56Time-Series Data57MongoDB Operations Framework
Tutorials/MongoDB/Change Streams
🍃MongoDB

Change Streams

Updated 2026-04-20
3 min read

Change Streams

Change streams are a powerful feature in MongoDB that allow you to listen for and respond to changes in real-time as they occur in your collections. This makes them ideal for applications that require near-instantaneous updates, such as real-time analytics, live dashboards, or event-driven architectures.

Introduction to Change Streams

A change stream is a cursor that provides a continuous flow of documents describing the changes to a collection. These changes can be inserts, updates, deletes, and more. You can use change streams to monitor data modifications and react accordingly.

Key Features

  • Real-time Data: Get notified about changes as they happen.
  • Flexible Filtering: Filter events based on specific criteria.
  • Resumability: Automatically resume from the last seen event in case of a failure or restart.
  • Cross-Database and Cross-Collection: Monitor changes across multiple databases and collections.

Setting Up Change Streams

To use change streams, you need to have MongoDB 3.6 or later installed. You can enable change streams on any replica set or sharded cluster.

Basic Usage

Here's a basic example of how to set up a change stream using the MongoDB Node.js driver:

const { MongoClient } = require('mongodb');

async function run() {
    const uri = "your_mongodb_connection_string";
    const client = new MongoClient(uri);

    try {
        await client.connect();
        const database = client.db('your_database_name');
        const collection = database.collection('your_collection_name');

        const changeStream = collection.watch();

        changeStream.on('change', (next) => {
            console.log(next);
        });
    } finally {
        await client.close();
    }
}

run().catch(console.dir);

Explanation

  1. MongoClient: Connect to your MongoDB instance using the connection string.
  2. Database and Collection: Specify the database and collection you want to monitor.
  3. Change Stream: Use collection.watch() to create a change stream.
  4. Event Handling: The change event is emitted whenever there's a change in the monitored collection.

Filtering Change Events

You can filter change events using the match option in the watch method. This allows you to specify criteria for which changes should be included in the stream.

Example: Filter by Operation Type

const changeStream = collection.watch([
    { $match: { "operationType": "insert" } }
]);

changeStream.on('change', (next) => {
    console.log("New document inserted:", next.fullDocument);
});

Explanation

  • $match: A MongoDB aggregation pipeline stage that filters documents.
  • operationType: Specifies the type of operation (insert, update, delete, etc.).

Handling Change Events

When a change event is detected, you can handle it in various ways depending on your application's requirements.

Example: Logging Changes

changeStream.on('change', (next) => {
    switch (next.operationType) {
        case 'insert':
            console.log("Inserted:", next.fullDocument);
            break;
        case 'update':
            console.log("Updated:", next.updateDescription.updatedFields);
            break;
        case 'delete':
            console.log("Deleted document with _id:", next.documentKey._id);
            break;
    }
});

Explanation

  • operationType: Determines the type of operation.
  • fullDocument: The full document after an insert or update.
  • updateDescription.updatedFields: Fields that were updated in a document.

Best Practices

  1. Error Handling: Always include error handling to manage any issues that arise during change stream processing.
  2. Resumability: Use the resumeToken to ensure your application can resume from where it left off after a restart or failure.
  3. Performance Considerations: Monitor and optimize performance, especially in high-throughput environments.
  4. Security: Ensure that only authorized users have access to change streams.

Example: Resuming Change Streams

let resumeToken;

async function run() {
    const uri = "your_mongodb_connection_string";
    const client = new MongoClient(uri);

    try {
        await client.connect();
        const database = client.db('your_database_name');
        const collection = database.collection('your_collection_name');

        let changeStream;
        if (resumeToken) {
            changeStream = collection.watch([], { resumeAfter: resumeToken });
        } else {
            changeStream = collection.watch();
        }

        changeStream.on('change', (next) => {
            console.log(next);
            resumeToken = next._id; // Update the resume token
        });

        await new Promise(resolve => setTimeout(resolve, 60000)); // Run for 1 minute
    } finally {
        await client.close();
    }
}

run().catch(console.dir);

Explanation

  • resumeAfter: Specifies a token to start streaming from a specific point.
  • _id: The resume token is stored in the _id field of each change event.

Conclusion

Change streams are a powerful feature in MongoDB that enable real-time data processing and monitoring. By understanding how to set up, filter, and handle change events, you can build applications that respond quickly to data changes. Always consider best practices for error handling, resumability, performance, and security to ensure your application is robust and efficient.

Further Reading

  • MongoDB Change Streams Documentation
  • Node.js Driver API Reference

This tutorial provides a comprehensive guide to using change streams in MongoDB, covering everything from basic setup to advanced filtering and handling techniques.


PreviousReal-Time ApplicationsNext Full-Text Search

Recommended Gear

Real-Time ApplicationsFull-Text Search