Introduction
Change streams let you watch for changes in your database in real time. This helps you react quickly when data updates happen.
const changeStream = db.watch(pipeline, options);const changeStream = db.watch();const changeStream = db.watch([{ $match: { operationType: 'insert' } }]);
const changeStream = db.watch([], { fullDocument: 'updateLookup' });
const changeStream = db.watch([{ $match: { operationType: 'insert' } }]); changeStream.on('change', (change) => { print('New document inserted:', JSON.stringify(change.fullDocument)); });
mydb?db.getSiblingDB('mydb').watch() method is called on the database object to start change streams.testdb database?
const changeStream = db.getSiblingDB('testdb').watch();
changeStream.forEach((change) => {
printjson(change.operationType);
});operationType is "insert".sales database:
const changeStream = db.sales.watch();
changeStream.forEach((change) => {
printjson(change);
});db.sales accesses a collection named 'sales' in the current database, not the database itself.db.getSiblingDB('sales').watch().orders database using change streams. Which code snippet correctly applies a filter to achieve this?$match.[{ $match: { operationType: 'update' } }] passed as an array to watch().Pass the test and this pattern moves unknown