-
Type: Task
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Empty show more show less
https://docs.mongodb.com/manual/changeStreams/index.html
The Open A Change Stream docs have a Node.js example
const collection = db.collection('inventory'); const changeStream = collection.watch(); changeStream.on('change', next => { // process next document } );
When I run this, I get
events.js:174 throw er; // Unhandled 'error' event ^ MongoError: no connection available to server cluster0-shard-00-00-ji2xi.mongodb.net:27017 at disconnectHandler (/Users/lauren/node_modules/mongodb/lib/core/topologies/server.js:264:14) at Server.command (/Users/lauren/node_modules/mongodb/lib/core/topologies/server.js:621:7) at AggregateOperation.executeCommand (/Users/lauren/node_modules/mongodb/lib/operations/command_v2.js:85:12) at AggregateOperation.execute (/Users/lauren/node_modules/mongodb/lib/operations/aggregate.js:96:11) at topology.selectServer (/Users/lauren/node_modules/mongodb/lib/operations/execute_operation.js:163:17) at _selectServer (/Users/lauren/node_modules/mongodb/lib/core/topologies/replset.js:1143:5) at ReplSet.selectServer (/Users/lauren/node_modules/mongodb/lib/core/topologies/replset.js:1146:3) at ReplSet.selectServer (/Users/lauren/node_modules/mongodb/lib/topologies/topology_base.js:363:32) at executeWithServerSelection (/Users/lauren/node_modules/mongodb/lib/operations/execute_operation.js:150:12) at executeOperation (/Users/lauren/node_modules/mongodb/lib/operations/execute_operation.js:81:16) Emitted 'error' event at: at processNewChange (/Users/lauren/node_modules/mongodb/lib/change_stream.js:535:43) at ChangeStreamCursor.<anonymous> (/Users/lauren/node_modules/mongodb/lib/change_stream.js:420:5) at ChangeStreamCursor.emit (events.js:198:13) at _next (/Users/lauren/node_modules/mongodb/lib/core/cursor.js:344:16) at self._initializeCursor (/Users/lauren/node_modules/mongodb/lib/core/cursor.js:750:9) at _initializeCursor (/Users/lauren/node_modules/mongodb/lib/change_stream.js:303:9) at done (/Users/lauren/node_modules/mongodb/lib/core/cursor.js:463:7) at executeOperation (/Users/lauren/node_modules/mongodb/lib/core/cursor.js:547:11) at err (/Users/lauren/node_modules/mongodb/lib/operations/execute_operation.js:76:14) at executeCallback (/Users/lauren/node_modules/mongodb/lib/operations/execute_operation.js:67:25)
There is an alternate code block that does work for me:
const changeStreamIterator = collection.watch(); const next = await changeStreamIterator.next();
I'm confused why collection.watch() would sometimes return a ChangeStream and sometimes return a ChangeStreamIterator as the docs above seem to indicate.
The driver docs say that watch() always returns a ChangeStream: https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#watch.
The driver docs also do not show an on() function for ChangeStream: https://mongodb.github.io/node-mongodb-native/3.3/api/ChangeStream.html.
Are the examples listed in the MongoDB doc outdated? Or is there a way to create a function that will be called whenever a new ChangeStream event is fired? From what I can tell there is no way to create a function that will be fired whenever a ChangeStream event is fired. Instead you have to create a function that constantly checks if there are new ChangeStream events.