with Python is there a way to listen for changes when insert or update is made in mongodb

百般思念 提交于 2019-12-11 03:25:46

问题


I am building a small system which throws data from a mongodb collection, it already works fine but I have to restart it everytime I make changes. I already have a monitor that dectect changes and restarts the server automatically but I want to do something like this with mongodb changes. I am currenlty using CentOs 5, Nginx, uWsgi & python2.7.


回答1:


I'd look into using tailable cursors, which remain alive after they've reached the end of a collection, and can block until a new object is available.

Using PyMongo, you can call Collection.find with a tailable=True option to enable this behavior. This blog post gives some good examples of its usage.

Additionally, instead of just querying the collection, which will only alert you to new objects added to that collection, you may want to query the database's oplog, which is a collection of all insert, updates, and deletes called on any collection in the database. Note that replication must be enabled for mongo to keep an oplog. Check out this blog post for info about the oplog and enabling replication.



来源:https://stackoverflow.com/questions/13808886/with-python-is-there-a-way-to-listen-for-changes-when-insert-or-update-is-made-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!