mongodb-node-changestreams-sample
mongodb-node-changestreams-sample copied to clipboard
MongoDB 3.6 Node Change Streams Sample
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
- Run
sh setup.shto set up your MongoDB 3.6 replica set using mlaunch - Run
sh run.shto start your MongoDB 3.6 replica
Node application configuration
- Run
npm installto install the required Node dependencies - Run
node produce.jsto create the demo database, the devices collection and a first document. - Run
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 thedevicescollection of thedemodatabase. - 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. - Start
node listen.jsagain and notice that the previous documents you just added get processed right away.