Python中的MongoDB通知 [英] MongoDB Notification in Python
问题描述
我正在将MongoDB与Python Web应用程序一起使用.我想知道从mongodb发出的通知,无论我在mongodb中进行了什么更改.我正在使用代码:
I am using MongoDB with Python web application. I want to know notification from mongodb whatever i changed in mongodb. i am using the code:
from pymongo import Connection
import time
MONGO_CONNECTION = MongoClient(settings.MONGO_CONNECTION_URL+"/"+settings.MONGO_DATABASE['NAME'], safe=True, j=True)
MONGO_DB = MONGO_CONNECTION[settings.MONGO_DATABASE['NAME']]
collection = MONGO_DB["id_64"]
cursor = collection.find(tailable=True)
while cursor.alive:
print "inside"
try:
doc = cursor.next()
print "new doc"
print doc
except StopIteration:
time.sleep(1)
我得到了错误:
OperationFailure: database error: Unable to execute query: error processing query: ns=scisphere.orgid_64 limit=0 skip=0
Tree: $and
Sort: {}
Proj: {}
tailable cursor requested on non capped collection
所以请帮我,我在做什么错.
So help me what is the wrong i am doing.
谢谢.
推荐答案
MongoDB 3.6 支持$changeStream
功能可监视集合中的更改.
MongoDB 3.6 supports $changeStream
feature to watch changes on a collection.
例如,使用与MongoDB 3.6兼容的PyMongo:
For example using PyMongo compatible with MongoDB 3.6:
for change in db.collection.watch():
print(change)
返回的ChangeStream在迭代过程中遇到潜在的可恢复错误时将自动恢复.恢复过程对应用程序是透明的,并确保没有丢失更改流文档.
The ChangeStream returned automatically resumes when it encounters a potentially recoverable error during iteration. The resume process is transparent to the application and ensures no change stream documents are lost.
另一个使用 PyMongo 监视集合中发生的所有插入的示例:
Another example to watch all inserts that are happening on a collection using PyMongo:
try:
for insert_change in db.collection.watch(
[{'$match': {'operationType': 'insert'}}]):
print(insert_change)
except pymongo.errors.PyMongoError:
# We know it's unrecoverable:
log.error('...')
这篇关于Python中的MongoDB通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!