I have multiple Python scripts writing to Mongodb using pyMongo. How can another Python script observe changes to a Mongo query and perform some function when the change occurs? mongodb is setup with oplog enabled.
相关问题
- how to define constructor for Python's new Nam
- MongoDB can not create unique sparse index (duplic
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
Query the oplog with a tailable cursor.
It is actually funny, because oplog-monitoring is exactly what the tailable-cursor feature was added for originally. I find it extremely useful for other things as well (e.g. implementing a mongodb-based pubsub, see this post for example), but that was the original purpose.
I ran into this issue today and haven't found an updated answer anywhere.
The Cursor class has changed as of v3.0 and no longer accepts the
tailable
andawait_data
arguments. This example will tail the oplog and print the oplog record when it finds a record newer than the last one it found.I wrote a incremental backup tool for MongoDB some time ago, in Python. The tool monitors data changes by tailing the
oplog
. Here is the relevant part of the code.Updated answer, pymongo 3
Also see http://api.mongodb.com/python/current/examples/tailable.html.
Original answer, pymongo 2
I had the same issue. I put together this rescommunes/oplog.py. Check comments and see
__main__
for an example of how you could use it with your script.