简体   繁体   English

PyMongo-使用for打印结果,但不显示

[英]PyMongo - Printing a result, using a for and it's not displaying

recently we've been learning Mongo and Python at University, however, in the latest assignment we were given, we're required to use PyMongo to use a pre-existent database of restaurants and perform various basic tasks. 最近,我们在大学里学习了Mongo和Python,但是在我们得到的最新作业中,我们需要使用PyMongo来使用预先存在的餐厅数据库并执行各种基本任务。

Due to a surgery, I couldn't assist to the lectures given, so I'm still a bit confused onto how to do certain things with PyMongo. 由于外科手术,我无法协助所给的讲座,因此我仍然对如何使用PyMongo做某些事情感到困惑。

One part of the task is write some querys to do stuff like "searching restaurants with latitudes lower than -90" and similar tasks. 任务的一部分是编写一些查询以完成诸如“搜索纬度低于-90的餐馆”之类的任务。

My code looks a bit like this: 我的代码看起来像这样:

conn = pymongo.MongoClient()
restDB = conn.restaurantes
doc = restDB.datos


prim = doc.find({"address.coord.0":{"$lt":-95.754168}}, {"name":True})
for resultado in prim:
    print resultado

#print prim

Whenever I do this, no results are displayed and there are no errors as well, so that's what confuses me. 每当执行此操作时,都不会显示任何结果,也不会出现任何错误,这就是让我感到困惑的原因。 Also trying to print "prim" gives me some .cursor message. 也尝试打印“ prim”会给我一些.cursor消息。

The basic template for querying a collection and iterating through the result with pymongo is: 用于查询集合并使用pymongo遍历结果的基本模板是:

from pymongo import MongoClient

with MongoClient() as client:
    db = client.get_database("my_db_name")
    coll = db.get_collection("my_collection_name")
    result_cursor = coll.find({"some_field":{"$lt":100}}, {"some_field":1})
    for doc in result_cursor:
        print doc

>>
{u'some_field': 3, u'_id': ObjectId('578d52a93ea71afa979e5737')}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM