【问题标题】:What happens to a pymongo cursor when all its elements have been iterated?当所有元素都被迭代时,pymongo 游标会发生什么?
【发布时间】:2015-11-13 01:20:26
【问题描述】:

我想使用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

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

【问题讨论】:

    标签: python-3.x pymongo


    【解决方案1】:

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

    默认情况下,服务器会在 10 点后自动关闭光标 不活动的分钟数或客户端是否已用尽光标。

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

    node_list = all_nodes[:]
    

    node_list = [node for node in all_nodes] 
    

    另见this question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-25
      • 2018-02-07
      • 2021-11-12
      • 1970-01-01
      • 2020-02-04
      • 2011-09-03
      • 1970-01-01
      • 2019-11-22
      相关资源
      最近更新 更多