简体   繁体   中英

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.

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.

对于简单的本地测试,您可以像这样运行mongodb

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM