我正在尝试使用以下代码获取 mongo 数据库中存在的一些 ID:
client = MongoClient('xx.xx.xx.xx', xxx)
db = client.test_database
db = client['...']
collection = db.test_collection
collection = db["..."]
for cursor in collection.find({ "$and" : [{ "followers" : { "$gt" : 2000 } }, { "followers" : { "$lt" : 3000 } }, { "list_followers" : { "$exists" : False } }] }):
print cursor['screenname']
print cursor['_id']['uid']
id = cursor['_id']['uid']
但是,过了一会儿,我收到了这个错误:
pymongo.errors.CursorNotFound: cursor id '...' not valid at server.
我找到了 article这是指那个问题。然而,我不清楚采取哪种解决方案。是否可以使用 find().batch_size(30)
?上面的命令具体是做什么的?我可以使用 batch_size
获取所有数据库 ID 吗?
最佳答案
您收到此错误是因为光标在服务器上超时(在 10 分钟不活动后)。
来自 pymongo 文档:
Cursors in MongoDB can timeout on the server if they’ve been open for a long time without any operations being performed on them. This can lead to an CursorNotFound exception being raised when attempting to iterate the cursor.
当您调用 collection.find
方法时,它会查询集合并将光标返回到文档。要获取文档,您需要迭代光标。当您遍历游标时,驱动程序实际上是在向 MongoDB 服务器发出请求以从服务器获取更多数据。每个请求返回的数据量由 batch_size()
方法设置。
来自 documentation :
Limits the number of documents returned in one batch. Each batch requires a round trip to the server. It can be adjusted to optimize performance and limit data transfer.
将 batch_size 设置为较低的值将帮助您解决超时错误错误,但它会增加您访问 MongoDB 服务器以获取所有文档的次数。
默认批量大小:
For most queries, the first batch returns 101 documents or just enough documents to exceed 1 megabyte. Batch size will not exceed the maximum BSON document size (16 MB).
没有通用的“正确”批量大小。您应该使用不同的值进行测试,看看适合您的用例的值是多少,即您可以在 10 分钟的窗口内处理多少个文档。
最后的手段是设置 no_cursor_timeout=True
。但是你需要确保在完成数据处理后光标是关闭的。
如何在没有try/except
的情况下避免它:
cursor = collection.find(
{"x": 1},
no_cursor_timeout=True
)
for doc in cursor:
# do something with doc
cursor.close()
关于python - pymongo.errors.CursorNotFound : cursor id '...' not valid at server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24199729/