pymongo没有查询输出
编辑: 我对这个问题进行了一些提炼。
mongo_documents = mongo_collection.find({"medicalObjectId": "269"})
print "\n\n"
for this_document in mongo_documents:
print this_document
print "-------------------------"
pqr = 269
mongo_documents2 = mongo_collection.find({"medicalObjectId": pqr})
print "\n\n"
for this_document2 in mongo_documents2:
print this_document2
我的问题是,我使用数字作为查询中的键的第一个代码块有效。但是在我使用变量的第二个块中,我没有得到任何输出。
我是 python 和 pymongo 的初学者,所以请耐心等待。
我有一个清单: row = [1, 2, ...., 100]
我想查询列表中每个条目的 mongodb 集合。 该集合的格式为: collection = {'pk', 'attribute1', 'attribute2', 'attribute3'}
我想调用 mongodb 连接并使用 row[i]=pk 迭代列表中的每个条目,并返回其他属性作为输出。
IE。 mongo_documents = mongo_collection.find({'pk' : row[0]}) mongo_documents = mongo_collection.find({'pk' : row[1]}) 等等。
我的代码是:
for row in result_set:
print row[0]
mongo_documents = mongo_collection.find({'medicalObjectId' : row[0]})
print mongo_documents
for this_document in mongo_documents:
print "----------------------------------"
print this_document
但是我没有得到任何输出。我哪里出错了? 如果我打印 mongo_documents,我得到
<pymongo.cursor.Cursor object at 0xe43150>
EDIT:
I have somewhat distilled the question.
mongo_documents = mongo_collection.find({"medicalObjectId": "269"})
print "\n\n"
for this_document in mongo_documents:
print this_document
print "-------------------------"
pqr = 269
mongo_documents2 = mongo_collection.find({"medicalObjectId": pqr})
print "\n\n"
for this_document2 in mongo_documents2:
print this_document2
My problem is that the first code chunk where I use the number as the key in the query, works. But the second chunk where I use the variable, i get no output.
I am a beginner at python and pymongo, so please bear with me.
I have a list as;
row = [1, 2, ...., 100]
I want to query a mongodb collection for each entry in my list.
The collection has the format:
collection = {'pk', 'attribute1', 'attribute2', 'attribute3'}
I want to call the mongodb connection and iterate through each entry in my list with row[i]=pk and return the other attributes as the output.
ie. mongo_documents = mongo_collection.find({'pk' : row[0]})
mongo_documents = mongo_collection.find({'pk' : row[1]})
and so on.
The code that I have is:
for row in result_set:
print row[0]
mongo_documents = mongo_collection.find({'medicalObjectId' : row[0]})
print mongo_documents
for this_document in mongo_documents:
print "----------------------------------"
print this_document
however i get no output. where am I going wrong?
if i print mongo_documents, i get
<pymongo.cursor.Cursor object at 0xe43150>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 mongodb 的 $in 运算符一次性获取所有行并迭代它们。
我没有测试过,如果不起作用,请在下面评论。
编辑
You could use the $in operator of mongodb to fetch all the rows at once and iterate through them.
I have not tested it, comment below if it doesnt work.
EDIT