Change Streams — MongoDB Technical Interview Questions & Answers
Reacting in real time to data changes.
Change Streams
- Simple meaning: Change streams let your app listen for inserts/updates/deletes in real time (needs replica set — Atlas has one).
- How to use: collection.watch() in a driver, then handle change.fullDocument.
- Good for live dashboards, cache busting, syncing search indexes.
- Common mistake: watch() on a standalone local mongod without a replica set — it fails.
- Interview tip: “Like a DB trigger over a stream. Requires replica set.”
Technical Interview Tip: Change stream = listen to CRUD events.
Code Examples for Change Streams
Change Streams — working example
const cursor = db.users.watch([
{ $match: { operationType: { $in: ["insert", "update"] } } }
])
while (cursor.hasNext()) {
printjson(cursor.next())
}Change streams emit real-time insert/update/delete events (replica set required).
How to Explain Change Streams to an Interviewer
Requirement for change streams?
Replica set (Atlas included)