简体   繁体   English

MongoDB无法创建“更改流”(java)

[英]MongoDB can't create 'change stream' (java)

I'm trying to get real-time updates from MongoDB. 我正在尝试从MongoDB获取实时更新。 I used the changes stream API (from here ). 我使用了更改流API(从此处开始 )。 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 ? replica sets是什么意思? How can I manage to fix this problem? 我该如何解决此问题?

This is the first time I'm using MongoDB... 这是我第一次使用MongoDB ...

Thanks. 谢谢。

A replica set is a group of MongoDB processes that maintain the data set. 副本集是一组维护数据集的MongoDB进程。 Replica Sets replicate data by creating a special collection called the oplog which records all modifications to the data. 复制集通过创建一个称为oplog的特殊集合来设置复制数据,该集合记录对数据的所有修改。

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. 有关部署副本集的指南,请参见https://docs.mongodb.com/manual/tutorial/deploy-replica-set/

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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