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.sh
to set up your MongoDB 3.6 replica set using mlaunch - Run
sh run.sh
to start your MongoDB 3.6 replica
- Run
npm install
to install the required Node dependencies - Run
node produce.js
to create the demo database, the devices collection and a first document. - Run
node listen.js
to start listening to change streams coming from the devices collection of the demo database. - In a separate Terminal window, run
node produce.js
again - this will add a document to thedevices
collection of thedemo
database. - Look at the window running
listen.js
and 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.js
process. 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.js
several times. - Start
node listen.js
again and notice that the previous documents you just added get processed right away.