pymongo没有查询输出

发布于 2024-12-10 08:41:45 字数 1266 浏览 1 评论 0原文

编辑: 我对这个问题进行了一些提炼。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空名 2024-12-17 08:41:45

您可以使用 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

You could use the $in operator of mongodb to fetch all the rows at once and iterate through them.

mongo_documents = mongo_collection.find({ 'medicalObjectId' : { '$in' : result_set } } );
for doc in mongo_documents:
    print mongo_documents

I have not tested it, comment below if it doesnt work.

EDIT

mongo_documents2 = mongo_collection.find({"medicalObjectId": str(pqr)}) 
print "\n\n" 
for this_document2 in mongo_documents2: 
    print this_document2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文