Pymongo - 尾随oplog

rea*_*dow 11 python mongodb pymongo

我正在尝试在mongo的oplog集合上实现pub/sub.提供的代码工作,没有 tailable = True选项设置(它将返回所有文档),但是一旦我将它传递给光标它就不会拾取任何东西(即使在所需的集合中进行了更改).

我正在使用pymongo 2.7.2

while(True):
    with self.database.connect() as connection:
        cursor = connection['local'].oplog.rs.find(
            {'ns': self.collection},
            await_data = True,
            tailable = True
        )

        cursor.add_option(_QUERY_OPTIONS['oplog_replay'])

        while cursor.alive:
            try:
                doc = cursor.next()

                print doc
            except(AutoReconnect, StopIteration):
                time.sleep(1)
Run Code Online (Sandbox Code Playgroud)

我尝试了很少的解决方案,但是只要添加了tailable选项,它仍然会失败.Oplog设置正确,因为mongo-oplog来自nodejs的模块按预期工作.

可能重复(没有接受的答案)

Ber*_*ett 6

您需要查询'ts'oplog字段,并在必须重新创建Cursor的情况下跟踪您读取的最后一个文档(通过时间戳).以下是您可以根据需要进行修改的示例:

import time

import pymongo

c = pymongo.MongoClient()
# Uncomment this for master/slave.
# oplog = c.local.oplog['$main']
# Uncomment this for replica sets.
oplog = c.local.oplog.rs
first = oplog.find().sort('$natural', pymongo.DESCENDING).limit(-1).next()
ts = first['ts']

while True:
    cursor = oplog.find({'ts': {'$gt': ts}}, tailable=True, await_data=True)
    # oplogReplay flag - not exposed in the public API
    cursor.add_option(8)
    while cursor.alive:
        for doc in cursor:
            ts = doc['ts']
            # Do something...
        time.sleep(1)
Run Code Online (Sandbox Code Playgroud)