Raise StopIteration无法在我的MongoDB上使用可裁剪光标

2024-06-08 23:07:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从我的数据库流新的条目,但我不断得到这个错误。 我从MongoDB的官方文档中获取了代码,并将其改编为我的数据库,但我不断遇到以下错误:

File "C:\Users\Davide\lib\site-packages\pymongo\cursor.py", line 1197, in next
    raise StopIteration
StopIteration

问题出在哪里?你知道吗

import time
from pymongo import MongoClient
import pymongo

client = MongoClient(port=27017)
oplog = client.local.oplog.rs
first = oplog.find().sort('$natural', pymongo.ASCENDING).limit(-1).next()
print(first)
ts = first['ts']

while True:
    # For a regular capped collection CursorType.TAILABLE_AWAIT is the
    # only option required to create a tailable cursor. When querying the
    # oplog the oplog_replay option enables an optimization to quickly
    # find the 'ts' value we're looking for. The oplog_replay option
    # can only be used when querying the oplog.
    cursor = oplog.find({'ts': {'$gt': ts}},
                        cursor_type=pymongo.CursorType.TAILABLE_AWAIT,
                        oplog_replay=True)
    while cursor.alive:
        for doc in cursor:
            ts = doc['ts']
            print(doc)
        # We end up here if the find() returned no documents or if the
        # tailable cursor timed out (no new documents were added to the
        # collection for more than 1 second).
        time.sleep(1)

Tags: thetoimport数据库forreplaydoc错误