如何找到kill_cursors()的cursor_id?

时间:2013-11-22 17:43:25

标签: python mongodb cursor pymongo

PyMongo API文档提到了一个杀死mongodb游标的函数:

kill_cursors(cursor_ids) Send a kill cursors message with the given ids.
Raises TypeError if cursor_ids is not an instance of list.
Parameters :     cursor_ids: list of cursor ids to kill

但是,它没有指定(据我所知)如何实际找出光标的id。我怎么能得到这个?

from pymongo import Connection
c = Connection()
crawl = c.db.coll.find()
print crawl.id? <-- no idea how to get this

2 个答案:

答案 0 :(得分:0)

您无需显式终止游标。一旦从光标中检索到所有结果,服务器就会自动清除游标。 (例如,使用for doc in collection.find()循环。)如果不检索所有结果,PyMongo会在取消引用其Cursor实例时自动杀死游标。例如: for doc in collection.find(): assert False # raise exception 当光标超出范围时,PyMongo会自动调用kill_cursors来清理服务器端的游标。

答案 1 :(得分:-1)

Jesse,如果我发现它是一个流氓光标,或者在测试过程中,我可能仍然希望显式地删除游标。在java脚本管理控制台中拥有该选项将是一个加号。除此之外,我们是否有办法列出mongo服务器上的所有打开游标?