我想使用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
光标只适合一次使用吗? (或者是其他事情继续发生?)如果我想要常规的数组行为,我该怎么做?
答案 0 :(得分:2)
是。光标只适合一次使用。如mongodb manual
中所述默认情况下,服务器将在10后自动关闭光标 几分钟不活动或客户端已用尽光标。
要使您输出为python列表,请使用
node_list = all_nodes[:]
或
node_list = [node for node in all_nodes]