Chapter 4: Resiliency [Change Streams]

Hello, Instructors!
@mattjavaly

I’ve tried to learn the " db.collection.watch()" method. And it works fine on my “M220” Sandbox cluster (Atlas cluster).

But when I tried to do it on my Home PC using mongodb 4.0.5 I had an error:

“The $changeStream stage is only supported on replica sets”

Of course I don’t use neither replication nor sharding on my “localhost” databases :grin:
Is there any way to use “db.collection.watch()” (or maybe some another way to track data changes) if I don’t use replication?

Thanks.

You don’t actually have to run multiple mongod instances to have a “replica set”, at least from the standpoint of having an OpLog, which is the only real requirement and what the $changeStream pipeline stage utilizes “under the hood”.

All you really need do is is either start your mongod with the --replSet option or edit the mongod.conf to include a replication section. i.e:

replication:
  replSetName: my-rs

Start or restart the server with these changes, and then connect with the mongo shell and enter:

rs.initiate()

Now you can write code that relies on an “Oplog” to be present.

There a fully detailed instructions for the same process in the official documentation:

Convert a Standalone to a Replica Set

3 Likes

Thanks a lot!
Now I’ve got it. :+1: