Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

MongoDB Change Streams Shell examples

Replica set setup

MongoDB Change Streams require a replica set to work (i.e. they don't work with a standalone MongoDB server). Here are the instructions to start your local, single-node MongoDB 3.6 replica set:

  1. Make sure MongoDB 3.6+ is installed and your machine and the MongoDB installation folder (containing tools such as mongo and mongod) is added to your local path.
  2. Run sh to start a test, single-node replica set (in the /data/rs1/db sub-folder).
  3. If you've started your replica set for the first time, run sh to initialize your replica set.
  4. Run mongo createProducts.js to create products (one every second) in the devices collection of the demo database in your replica set. All of the documents created with that script have a quantity attribute greater or equal than 11.
  5. In a separate Terminal console, start sh to monitor inserts of the products into your database. Note that the script logs the first change it captures, simulates an app failure for 10 seconds and then catches up the inserts it missed (using a resume token).
  6. Stop the script and the mongo createProducts.js process (with Ctrl+C).
  7. Run sh in a Terminal console to monitor quantity updates of documents in the stock collection. Specifically, this change stream only prints out document updates when the quantity attributes is set to a value lower or equal than 10.
  8. Run mongo updateProducts.js to trigger quantity updates below 10 and watch changes being logged in the sh window.
You can’t perform that action at this time.