Roi Amiel
Roi Amiel

Reputation: 365

MongoDB can't create 'change stream' (java)

I'm trying to get real-time updates from MongoDB. I used the changes stream API (from here). This is my code:

collection.watch().forEach(do_somthing);

But it just throws this error:

The $changeStream stage is only supported on replica sets

What is the meaning of replica sets? How can I manage to fix this problem?

This is the first time I'm using MongoDB...

Thanks.

Upvotes: 0

Views: 2028

Answers (2)

wutzebaer
wutzebaer

Reputation: 14865

For a simple local test you can run your mongodb like that

mongod --dbpath=d:\data --oplogSize 50 --replSet rs0

Upvotes: 0

Adam Harrison
Adam Harrison

Reputation: 3421

A replica set is a group of MongoDB processes that maintain the data set. Replica Sets replicate data by creating a special collection called the oplog which records all modifications to the data.

Change streams work by reading the oplog, so they require that your deployment are configured as a replica set.

See https://docs.mongodb.com/manual/tutorial/deploy-replica-set/ for a guide on deployment a replica set.

Upvotes: 1

Related Questions