python-3.x - 当 pymongo 游标的所有元素都被迭代后,它会发生什么?

标签 python-3.x pymongo

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

    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

光标只能用于一种用途吗? (或者发生了其他事情?)如果我想要常规的数组行为,我该如何得到它?

最佳答案

是的。光标只能用于一种用途。如 mongodb manual 中所述

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

要让你输出为 python 列表,请使用

node_list = all_nodes[:]

node_list = [node for node in all_nodes] 

另请参阅this question

关于python-3.x - 当 pymongo 游标的所有元素都被迭代后,它会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32100631/

相关文章:

python - 如何在Python中检查单行if else if else条件

python - 使用 MongoDB 管理 Python 多处理

python - (长)从列表中的字符串中删除单引号

flask - pymongo 中的唯一字段

python - mongo 中的聚合查询有效,Pymongo 中没有

python bson.errors.InvalidDocument : Cannot encode object: datetime. 日期 (2015, 3, 1)

mongodb - 在mongodb中查找具有最大值的不同文档

python-3.x - GnuPG 已安装,但 python 在运行时未找到它

python-3.x - 叶 map 模块,试图为标记颜色提供更多选项

python - 在 python 3.x 中有效地搜索多个文件的关键字的最佳方法?