Change Streams
Change Streams: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.
On this page
MongoDB Tutorial · Lesson 84 of 100
Change Streams
Foundations & CRUD ✓ → Queries & Schema ✓ → Aggregation & Scale ✓ → Atlas & Projects
Atlas & Projects · 4 — Build · ~10 min · MongoDB — Modern Features
What is this?
Change streams let applications watch insert/update/delete/replace events on a collection, database, or cluster in real time using the oplog under the hood. Requires a replica set.
Why should you care?
Chat fan-out, cache invalidation, and event-driven microservices need “tell me when data changes” without polling.
See it live — copy this example
Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.
// mongosh / Node concept — watch new chat messages
const stream = db.messages.watch([
{ $match: { "operationType": "insert", "fullDocument.roomId": "lobby" } }
], { fullDocument: "updateLookup" })
// In Node:
// const change = await stream.next();
// change.fullDocument contains the new message
db.messages.insertOne({ roomId: "lobby", text: "hi", at: new Date() })
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- watch opens a cursor on changes.
- The pipeline filters to inserts for room lobby.
- Inserting a message emits an event the app can push over WebSockets.
- Resume tokens allow restart after disconnects.
Practice next
- Ensure you are on a replica set (Atlas is).
- Open a change stream on messages in a Node script.
- Insert a document in another shell and see the event.
- Watch update operations on orders.status.
- Add a $project stage in the watch pipeline.
Remember
Change streams push data-change events. Need replica set / Atlas. Filter early; resume carefully.
Live order tracking
When status flips to shipped, a change stream notifies the mobile push service.
Outcome: Customers get “out for delivery” without the app polling Mongo.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!