当 pymongo 游标的所有元素都被迭代时,它会发生什么? [英] What happens to a pymongo cursor when all its elements have been iterated?

查看:42
本文介绍了当 pymongo 游标的所有元素都被迭代时,它会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 pymongo 从我的数据库中获取一组条目.它似乎返回一个光标".我不知道那是什么.

I wanted to use pymongo to get an array of entries from my database. It seems to return a "cursor" instead. I don't know what that is.

    all_nodes = pymongo.MongoClient("mongodb://localhost")["provemath"]["nodes"].find(None)

    print('ALL NODES')
    for node in all_nodes:
        print(node)

    print('STILL NODES')
    for node in all_nodes:
        print(node)

有输出:

ALL NODES
{'_notes': [], '_examples': [], '_type': 'definition', '_plural': None, '_counterexamples': [], '_intuitions': [], '_id': 'unique', '_importance': 4, '_name': 'unique', '_dependencies': [], '_description': 'An occurence of a property is __unique__ if the property occurs exactly $1$ time.'}
{'_notes': ['Vertices are often drawn as a dot.  They are also called *nodes*.'], '_examples': [], '_type': 'definition', '_plural': '__vertices__', '_counterexamples': [], '_intuitions': [], '_id': 'vertex', '_importance': 4, '_name': 'vertex', '_dependencies': ['unique'], '_description': 'A __vertex__ is a fundamental unit used to create graphs.'}
{'_notes': ['It is possible that $a=b$.'], '_examples': [], '_type': 'definition', '_plural': None, '_counterexamples': [], '_intuitions': ['Edges are usually drawn as a line or curve connecting two vertices.  In the case that $a=b$, the edge is drawn as a small loop that connects $a$ to itself.'], '_id': 'edge', '_importance': 4, '_name': 'edge', '_dependencies': ['unique', 'vertex'], '_description': 'An __edge__ is a set ${a,b}$ where $a$ and $b$ are vertices.'}
{'_notes': [], '_examples': [], '_type': 'definition', '_plural': None, '_counterexamples': [], '_intuitions': [], '_id': 'loop', '_importance': 4, '_name': 'loop', '_dependencies': [], '_description': 'A __loop__ is an edge $e = {a,a} = {a}$.'}
STILL NODES

游标只能用于一种用途吗?(或者还有其他事情发生吗?)如果我想要常规的数组行为,我将如何获得?

Is a cursor only good for one use? (or is something else going on?) If I wanted regular array behavior, how would I get that?

推荐答案

是的.光标仅适用于一种用途.如 mongodb 手册

Yes. Cursor is only good for one use. As described in mongodb manual

默认情况下,服务器会在 10 秒后自动关闭游标几分钟的不活动或客户端已用尽游标.

By default, the server will automatically close the cursor after 10 minutes of inactivity or if client has exhausted the cursor.

为了让你输出为 python 列表使用

To make you output as python list use

node_list = all_nodes[:]

node_list = [node for node in all_nodes] 

另见这个问题

这篇关于当 pymongo 游标的所有元素都被迭代时,它会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆