NodeJS 和 MongoDB:有没有办法监听集合并在集合有新文档时调用回调?
有没有办法监听 MongoDB 集合并在集合有新文档时触发回调?
Is there a way to listen to a MongoDB collection and have a callback gets triggered when a collection has a new document?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来还没有办法。在“触发”JIRA 中有很多关于相关主题的讨论:
https://jira.mongodb.org/browse/SERVER-124
您可以解决此问题这是通过使用时间戳或计数进行轮询,但事件回调显然会更好。
Looks like there isn't a way yet. There is a lot of discussion in the "triggers" JIRA about related topics:
https://jira.mongodb.org/browse/SERVER-124
You can work around this by polling with timestamps or counts, but an event callback would obviously be better.
数据库没有任何主动推送,但您可以连接到复制。
假设您有一个 副本集 (您不会运行单个 mongod,你?)。
每个更改都会写入主服务器上的 oplog,然后被复制到辅助节点。
您可以使用 可尾游标。注意,这仍然是拉,而不是推。
There aren't any active pushes from the DB, but you could hook into replication.
Let's assume you have a replica set (you wouldn't run single mongod, would you?).
Every change is written to the oplog on the primary and then is replicated to secondaries.
You can efficiently pull new changes (both inserts and updates) from the oplog, using tailable cursors. Note, this is still pull, not push.