MongoDB & NoSQL (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
Change Streams: Real-time Event-Driven Architectures.
senior LevelCode ExampleSystem Design
In Simple Words: Change Streams allow applications to subscribe to real-time data modifications at the collection, database, or deployment level without polling:
- Powered by tailing the replication Oplog.
- Emits insert, update, replace, and delete events directly into WebSockets, microservices, or Kafka topics.
javascript
const collection = db.collection('orders');
const changeStream = collection.watch([{ $match: { 'fullDocument.status': 'paid' } }]);
changeStream.on('change', (next) => {
console.log('New paid order received:', next.fullDocument);
triggerNotificationService(next.fullDocument);
});