Table of contents
1.
Introduction
2.
MongoDB Change Streams
2.1.
Need for Change Streams
2.2.
How to use Change Streams?
2.2.1.
Code
2.2.2.
Output
2.2.3.
Explanation
3.
Features of Change Streams
3.1.
Flexibility
3.2.
Scalability
3.3.
In-order and non-stop stream
3.4.
Filtering
3.4.1.
Code
3.4.2.
Output
3.4.3.
Explanation
3.5.
Security
4.
Use Cases of Change Streams
5.
Frequently Asked Questions
5.1.
How can we close a Change Stream?
5.2.
When is polling more useful than Change Streams?
5.3.
When is reading the oplog a better option than Change Streams?
5.4.
What is a real-time database?
5.5.
Is MongoDB a real-time database?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

MongoDB Change Streams

Author Riya Singh
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Introduction

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:

  1. They enable immediate response and enhance the user experience.
     
  2. 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.
     
  3. 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);
You can also try this code with Online Javascript Compiler
Run Code

Output

Output showing change event

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.

Features of Change Streams

Change Streams offer many powerful features, such as the following.

Flexibility

Change Streams are flexible as they allow the users to choose if they want only the updated fields or the entire document. This leads to better bandwidth usage.

Scalability

Change Streams can handle a high volume of data efficiently. Thus, Change Streams are scalable.

In-order and non-stop stream

Change Streams ensure that the events are delivered in the order of occurrence and provide a reliable stream even after failures. They also allow users to start the stream where it was last disconnected, ensuring no changes are lost. This is done because each response comes with a resume token, which can be given as an argument to the change stream when starting a new change stream.

Filtering

Change Streams allow change event filtering using the aggregation pipeline stages. For example, we can use the $match stage to filter the stream by the operationType.

Let us understand this using an example.

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 pipeline = [
		{ $match: { operationType: 'insert' } }
	];

	// Modify Change Stream Output using Aggregation Pipeline
	const changeStream = collection.watch(pipeline);

	console.log('Change Stream Setup Done!');

	// Listen for insert events
	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);
You can also try this code with Online Javascript Compiler
Run Code

Output

We use the MongoDB Cloud website for manually inserting a document.

MongoDB Cloud website with Insert Document Option

The output of this code on receiving an insert event is shown below.

Output of Change Stream modified by Aggregate Pipeline

Explanation

The above code sets up a change stream for listening to the insert events only. All other operation types, like update, delete, etc., are ignored by this change stream. The aggregation pipeline is passed as an argument to the watch() method, which allows modifying the output of the change stream.

Security

Change Streams are secure, as only users with read access to the collection can make a change stream on that collection. Authorization and Authentication can be used to enhance security.

Use Cases of Change Streams

Change streams allow tracking of real-time changes without polling the database periodically or accessing the operations log. Some common uses of Change Streams are:
 

  1. Change Streams can detect changes in the financial market and automatically execute trades based on predefined conditions. They can also send notifications to subscribers regarding risks and opportunities in the market.
     
  2. Change Streams can also be used in IoT applications. For example, when an RFID card is scanned on the scanner, the attendance is marked in the database, and this update can be detected, and a notification can be sent to the parents about the student's attendance.
     
  3. Change Streams can detect and notify about likes, comments on posts etc.
     
  4. Change Streams can update a player's real-time performance in a game and change their position in the leaderboard.

Frequently Asked Questions

How can we close a Change Stream?

We can close a change stream by calling the close() method on the change stream object. This terminates the connection, and no more changes are tracked.

When is polling more useful than Change Streams?

Polling is more useful than Change Streams when data is not time-sensitive or changes frequently. For example, the weather data can be updated every 10 minutes by polling instead of real-time updates.

When is reading the oplog a better option than Change Streams?

Reading the oplog is a better option than Change Streams when we need to access the changes that occurred before the change stream was created or when we need to access changes not supported by change streams.

What is a real-time database?

A real-time database is a database that can process and deliver data updates to applications as they occur without delays or polling.

Is MongoDB a real-time database?

MongoDB is not a real-time database by default, but it can support real-time data changes using change streams.

Conclusion

This article explored MongoDB Change Streams. Change Streams provide a powerful way to track changes in the database without polling or reading the oplog. Change Streams offer several features and benefits, making them easy to use.

We recommend reading the following articles to learn more about Streams and MongoDB:

You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Happy Coding!

Live masterclass