编辑: 我有点提炼了这个问题。
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遍历列表中的每个条目,并返回其他属性作为输出。
即。 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>
答案 0 :(得分:4)
您可以使用mongodb的$ in运算符一次获取所有行并迭代它们。
mongo_documents = mongo_collection.find({ 'medicalObjectId' : { '$in' : result_set } } );
for doc in mongo_documents:
print mongo_documents
我没有测试过,如果它不起作用,请在下面评论。
修改
mongo_documents2 = mongo_collection.find({"medicalObjectId": str(pqr)})
print "\n\n"
for this_document2 in mongo_documents2:
print this_document2