MongoDB 3.6 Change Streams Node Sample
MongoDB 3.6 cluster configuration
You must configure a replica set for change streams to work. Follow the instructions below:
- Make sure the latest version of mtools is installed on your machine
- Edit setup.sh and update the MONGODB_LOCATION variable to point to your local MongoDB 3.6 installation root
sh setup.shto set up your MongoDB 3.6 replica set using mlaunch
sh run.shto start your MongoDB 3.6 replica
Node application configuration
npm installto install the required Node dependencies
node produce.jsto create the demo database, the devices collection and a first document.
node listen.jsto start listening to change streams coming from the devices collection of the demo database.
- In a separate Terminal window, run
node produce.jsagain - this will add a document to the
devicescollection of the
- Look at the window running
listen.jsand if everything was properly configured you should get a Change Stream log message, followed by a waiting for change stream... message.
- Now stop the
listen.jsprocess. We're simulating an application crash and the ability to resume processing MongoDB Change Streams from the exact time the application "crashed".
- Add a few documents by running
node produce.jsseveral times.
node listen.jsagain and notice that the previous documents you just added get processed right away.