Change streams let applications watch for changes like inserts, updates, or deletes in real time. This avoids repeatedly asking the database for updates (polling), making apps more efficient and responsive.
db.collection.watch()
db.collection.insertOne({name: 'Alice', age: 30})The watch() method returns a stream of change event documents. When a document is inserted, the event includes operation type, document key, and the full inserted document.
The correct method to open a change stream is watch(). Other options are not valid MongoDB methods.
You can pass an aggregation pipeline to watch() with a $match stage to filter events, such as updates affecting the 'status' field.
Change streams require MongoDB to run as a replica set or sharded cluster. On standalone servers, attempting to open a change stream causes a MongoServerError.