Skip to content

guumaster/mongodb-change-stream

Repository files navigation

Mongo Change Stream POC

This repo shows how to watch changes in a collection with the ChangeStream API.

It consists of a mongo database with two nodes in replicaset, a watcher container that prints all documents inserted into the "logs" collection, and a generator container that generate random messages and make bulk inserts into the same "logs" collection. The generator container can be started with multiple replicas.

Requirements

Usage

Setup Mongo replica set

  1. First you need to start mongo in replicaset. Start the nodes:

docker-compose up -d mongo0 mongo1

  1. Connect to one nodo and set the configuration:

docker exec -it mongo0 mongo

  1. Once inside Mongo shell console, set the config and initiate the replicaset:
config={"_id":"rs0","members":[{"_id":0,"host":"mongo0:27017"},{"_id":1,"host":"mongo1:27017"}]}
rs.initiate()

Clone nats-streaming-ui

git clone --depth=1 [email protected]:united-travel-tickets/common/nats-streaming-ui.git 

Start the watcher

Start the ChangeStream watcher container:

docker-compose up mongo-watcher-socket

Start the generators

You can start multiple generators with the scale command:

docker scale nats-generator=10

Set scale to 0 if you want to stop inserting messages into the logs collection

Open logs UI

http://localhost:5000

References

About

Watch mongo changes with Golang

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published