Q36: What are Change Streams and how do they work in real-time applications?
Change Streams allow applications to listen for real-time data changes across a collection, database, or deployment without polling. They leverage the replication oplog (oplog.rs) to emit change notification events (insert, update, delete, replace).
Setting up a Change Stream in Node.js
const changeStream = User.watch();
changeStream.on('change', (change) => {
console.log('Real-time DB Event:', change.operationType, change.fullDocument);
});💡 Subscribes to live database modifications using Mongoose's model watch wrapper.
Key Takeaways
- Provides real-time event notifications via the replication oplog.
- Requires a Replica Set or Sharded Cluster.
Knowledge Check
Which internal MongoDB component powers real-time Change Stream event notifications?