Introduction
Change stream pipelines let you watch only the database changes you care about. This saves time and resources by filtering out unwanted updates.
db.collection.watch([pipeline], { options })db.orders.watch([
{ $match: { operationType: 'insert' } }
])db.users.watch([
{ $match: { 'fullDocument.status': 'active' } }
])db.products.watch([
{ $match: { operationType: { $in: ['update', 'replace'] } } }
])const changeStream = db.orders.watch([ { $match: { operationType: 'insert' } } ]); changeStream.on('change', (change) => { print('New order inserted with id:', change.fullDocument._id); });
$match stage in a MongoDB change stream pipeline?$match in pipelines$match stage filters these change events so only specific changes are passed through.$match filters events [OK]$match?operationType to specify the type of change, and 'insert' is the value for insert operations.{ $match: { operationType: 'insert' } } to watch only insert changes.[ { $match: { 'fullDocument.status': 'active' } } ]fullDocument.status field equals 'active'.[ { $match: { operationType: 'delete' } } ]operationType is correct and the filter matches delete events properly.category is 'books' and the operation is either 'insert' or 'update'. Which pipeline correctly filters these events?$in inside $and to filter operationType for both 'insert' and 'update'.Pass the test and this pattern moves unknown