Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
// Watch a collection for all changes const collection = client.db('shop').collection('orders') const changeStream = collection.watch() changeStream.on('change', (event) => { console.log('Operation:', event.operationType) // insert | update | delete console.log('Document key:', event.documentKey) if (event.operationType === 'insert') { console.log('New order:', event.fullDocument) } if (event.operationType === 'update') { console.log('Updated fields:', event.updateDescription.updatedFields) } }) // Watch with a pipeline filter — only listen for inserts const insertStream = collection.watch([ { $match: { operationType: "insert" } } ]) // Resumable stream — store resume token and restart later let resumeToken changeStream.on('change', (event) => { resumeToken = event._id // save this to persistent storage processEvent(event) }) // Resume from where we left off const resumedStream = collection.watch([], { resumeAfter: resumeToken })
Result
Open