Is there a way for Node.js to listen to a change in a particular data in a collection of MongoDB, and fire an event if a change happens?
相关问题
- MongoDB can not create unique sparse index (duplic
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Spring Data MongoDB - lazy access to some fields
- google-drive can't get push notifications
- Golang mongodb aggregation
相关文章
- node连接远程oracle报错
- mongodb有没有什么办法禁止读取数据的时候进行缓存
- mongodb-aggregate聚合查询分组后如何获得多字段
- mongodb error: how do I make sure that your journa
- How can make folder with Firebase Cloud Functions
- How to track MongoDB requests from a console appli
- @angular-cli install fails with deprecated request
- node.js modify file data stream?
MongoDB 3.6 introduced change streams, which are designed to solve precisely this problem.
Here's an example: https://github.com/thakkaryash94/mongodb-change-streams-nodejs-example
Well, this is an old question, but I was struggling with the same thing. I found a number of tidbits that helped me put together a solution, and I've published it as a library:
https://github.com/TorchlightSoftware/mongo-watch
The library is written in coffeescript. Here's an example in javascript, for those that prefer.
Well, kind of late.
But still, if someone looking to notify the MongoDB changes to the node.js then they can use mubsub library.
This is active library and very easy to integrate with nodejs. It uses Mongo's tailable cursors and capped collections.
It works in pub/sub fashion to notify the subscriber when the document inserted into the MongoDB.
Check on Github for details.
MongoDB apparently now supports triggers and watch, this answer is outdated.
[Original] I believe you are looking for a database trigger.
Unfortunately, MongoDB has no support for them yet, so I don't think you can listen for changes directly from the database. You'll need to setup some sort of notification system (e.g. pub/sub) that alerts interested parties when a collection has changed.