使用Python,有一种方法可以在mongodb中进行插入或更新时侦听更改 [英] with Python is there a way to listen for changes when insert or update is made in mongodb

查看:113
本文介绍了使用Python,有一种方法可以在mongodb中进行插入或更新时侦听更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个从mongodb集合中抛出数据的小型系统,它已经可以正常工作,但是每次进行更改时都必须重新启动它. 我已经有了一个可以检测更改并自动重新启动服务器的监视器,但是我想对mongodb更改执行类似的操作. 我目前正在使用CentO 5,Nginx,uWsgi& python2.7.

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.

推荐答案

我会考虑使用

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.

使用 PyMongo ,您可以使用tailable=True选项调用Collection.find以启用此行为. 博客文章提供了一些用法示例.

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.

此外,您可能想查询数据库的 oplog ,而不仅仅是查询集合(它只会向您提醒添加到该集合的新对象),而不是查询该集合.数据库中任何集合上调用的所有插入,更新和删除的集合.请注意,必须启用复制才能使mongo保持操作日志.查看此博客文章以获得有关oplog和启用的信息复制.

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.

这篇关于使用Python,有一种方法可以在mongodb中进行插入或更新时侦听更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆