Introduction
Hello Ninjas! You must be aware that effective data management is the heart of modern applications. MongoDB is a popular NoSQL database that uses a document-based data storage approach. One of the many powerful features offered by MongoDB is Change Streams. Change Streams allow us to capture, track and react to changes in the database in real time.

In this article, we will explore what MongoDB Change Streams are, their uses, features, etc. This will allow you to understand and integrate change streams into your projects!
MongoDB Change Streams
A MongoDB Change Stream is a stream of change events. A change event represents a change in the MongoDB database. It consists of a unique identifier, a namespace (identifying the collection), the operation type (insert, delete, update, etc.) and the document after the change. Applications can subscribe to these change streams and then listen to the change events.
Change Streams are available in MongoDB 3.6 and later.
Need for Change Streams
Even though both Change Streams and the oplog (operations log) can capture and track changes in the database, Change Streams offer more advantages over the operations log. The oplog (operations log) in MongoDB is a special collection which records all operations that modify the database. It has a fixed maximum size and stores a rolling history of operations (gets overwritten). It can also be used to troubleshoot problems or recover some lost data.
Change Streams offer a more user-friendly way to track changes using an API, abstracting away the complex working of oplog. Reading the oplog also has a higher overhead and requires writing complex logic to handle data consistency, error handling, and resume logic. Change Streams does all of this automatically for us.
Change Streams have many benefits:
-
They enable immediate response and enhance the user experience.
-
It is more efficient than constantly polling the database for changes. Constant polling leads to a waste of a lot of resources. Polling is the technique of querying the database periodically to check for changes.
- Change Streams provide a more straightforward way to track the changes.
How to use Change Streams?
For setting up a change stream, first of all, you need to connect to your MongoDB database using a MongoDB driver of the programming language of your choice. In this article, we will use NodeJs.
We will use the watch() method on a collection, database or deployment to open a change stream. Using an example, let us look at the way to open a change stream.
Code
const { MongoClient } = require('mongodb');
const dotenv = require('dotenv');
dotenv.config();
async function startChangeStream(client) {
const database = client.db('sample_mflix');
const collection = database.collection('movies');
const changeStream = collection.watch();
console.log('Change Stream Setup Done!');
// Listen for all changes
changeStream.on('change', (change) => {
console.log('Change event:');
console.log(change);
});
}
async function main() {
try {
// Replace with your MongoDB connection string
const uri = `mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@cluster0.1tdgqep.mongodb.net/`;
const client = new MongoClient(uri);
await client.connect();
console.log('Connected to MongoDB Server');
await startChangeStream(client);
} catch (error) {
console.error('Error connecting to MongoDB:', error);
process.exit(1);
}
}
main().catch(console.error);
Output

Explanation
In this example, we use a sample dataset from MongoDB containing the database sample_mflix and the collection movies in it.
In the main() function, we establish a connection to the MongoDB database using the connection string. The connection details, like username and password, are stored in a separate .env file, and the dotenv package is used.
The watch() method creates a change stream object. An event listener is set up on the change stream object, which tracks all the change events in the movies collection. Whenever a change occurs, it is logged into the console.
As we can see in the output, an update event can be seen. The event has an identifier (unique), an operationType field (stating the type of change operation) and other related attributes.