Python 当我试图通过pymongo查询我的mongodb时,为什么会得到一个pymongo.cursor.cursor?
我在mongodb数据库中使用了大量推文。我想用pymongo查询这些推文。例如,我想查询屏幕名称。但是,当我尝试这样做时,python不会返回tweet,而是返回一条关于pymongo.cursor.cursor的消息。这是我的密码:Python 当我试图通过pymongo查询我的mongodb时,为什么会得到一个pymongo.cursor.cursor?,python,mongodb,twitter,cursor,pymongo,Python,Mongodb,Twitter,Cursor,Pymongo,我在mongodb数据库中使用了大量推文。我想用pymongo查询这些推文。例如,我想查询屏幕名称。但是,当我尝试这样做时,python不会返回tweet,而是返回一条关于pymongo.cursor.cursor的消息。这是我的密码: import sys import pymongo from pymongo import Connection connection = Connection() db = connection.test tweets = db.tweets list(twe
import sys
import pymongo
from pymongo import Connection
connection = Connection()
db = connection.test
tweets = db.tweets
list(tweets.find())[:1]
我得到一个JSON,如下所示:
{u'_id': ObjectId('51c8878fadb68a0b96c6ebf1'),
u'contributors': None,
u'coordinates': {u'coordinates': [-75.24692983, 43.06183036],
u'type': u'Point'},
u'created_at': u'Mon Jun 24 17:53:19 +0000 2013',
u'entities': {u'hashtags': [],
u'symbols': [],
u'urls': [],
u'user_mentions': []},
u'favorite_count': 0,
u'favorited': False,
u'filter_level': u'medium',
u'geo': {u'coordinates': [43.06183036, -75.24692983], u'type': u'Point'},
u'id': 349223725943623680L,
u'id_str': u'349223725943623680',
u'in_reply_to_screen_name': None,
u'in_reply_to_status_id': None,
u'in_reply_to_status_id_str': None,
u'in_reply_to_user_id': None,
u'in_reply_to_user_id_str': None,
u'lang': u'en',
u'place': {u'attributes': {},
u'bounding_box': {u'coordinates': [[[-79.76259, 40.477399],
[-79.76259, 45.015865],
[-71.777491, 45.015865],
[-71.777491, 40.477399]]],
u'type': u'Polygon'},
u'country': u'United States',
u'country_code': u'US',
u'full_name': u'New York, US',
u'id': u'94965b2c45386f87',
u'name': u'New York',
u'place_type': u'admin',
u'url': u'http://api.twitter.com/1/geo/id/94965b2c45386f87.json'},
u'retweet_count': 0,
u'retweeted': False,
u'source': u'<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>',
u'text': u'Currently having a heat stroke',
u'truncated': False,
u'user': {u'contributors_enabled': False,
u'created_at': u'Fri Oct 28 02:04:05 +0000 2011',
u'default_profile': False,
u'default_profile_image': False,
u'description': u'young and so mischievious',
u'favourites_count': 1798,
u'follow_request_sent': None,
u'followers_count': 368,
u'following': None,
u'friends_count': 335,
u'geo_enabled': True,
u'id': 399801173,
u'id_str': u'399801173',
u'is_translator': False,
u'lang': u'en',
u'listed_count': 0,
u'location': u'Upstate New York',
u'name': u'Joe Catanzarita',
u'notifications': None,
u'profile_background_color': u'D6640D',
u'profile_background_image_url': u'http://a0.twimg.com/profile_background_images/702001815/f87508e73bbfab8c8c85ebe10b29fcf6.png',
u'profile_background_image_url_https': u'https://si0.twimg.com/profile_background_images/702001815/f87508e73bbfab8c8c85ebe10b29fcf6.png',
u'profile_background_tile': True,
u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/399801173/1367200323',
u'profile_image_url': u'http://a0.twimg.com/profile_images/378800000012256721/d8b5f801fb331de6ead4aed42dc77a46_normal.jpeg',
u'profile_image_url_https': u'https://si0.twimg.com/profile_images/378800000012256721/d8b5f801fb331de6ead4aed42dc77a46_normal.jpeg' ,
u'profile_link_color': u'140DE0',
u'profile_sidebar_border_color': u'FFFFFF',
u'profile_sidebar_fill_color': u'E0F5A6',
u'profile_text_color': u'120212',
u'profile_use_background_image': True,
u'protected': False,
u'screen_name': u'JoeCatanzarita',
u'statuses_count': 6402,
u'time_zone': u'Quito',
u'url': None,
u'utc_offset': -18000,
u'verified': False}}
你知道我做错了什么/我怎样才能让pymongo回复我想要的推文吗
谢谢 我认为问题在于“screen\u name”在子文档中,如果您可以提供我可能能够帮助您的文档结构的话。PyMongo的find()方法返回一个游标。要在服务器上实际执行查询并检索结果,请使用
list
或for循环迭代光标:
for doc in tweets.find({'screen_name': 'name'}):
print(doc)
# Or:
docs = list(tweets.find({'screen_name': 'name'}))
如果tweets.find({“screen\u name”:“name”}).count()返回0,则表示没有与您的查询匹配的文档
编辑:现在您已经发布了一个示例文档,我看到您希望进行如下查询:
list(tweets.find({'user.screen_name': 'name'}))
。。。由于screen\u name
字段嵌入到user
子文档中。好,现在我知道您的问题是什么了:
如果仔细查看文档,您会注意到“screen_name”在子文档用户中,因此如果您想访问它,您需要做的只是以下操作:
tweets.find({"user.screen_name": "JoeCatanzarita"}) #for example.
无论何时,如果您试图查找的元素位于子文档中(如在这种情况下或数组中),请始终使用此语法。我在collection.find()调用中遇到了同样的问题
我检查了对象的类型,它是python dict。所以我拿了dict并对它进行了迭代,尽管只有一个条目,而且她工作得非常出色
myResult = db.find({}, {<!-- blah blah blah for the fields you want -->}).sort({"_id":1}).limit(1)
for item in myResult:
print item
myResult=db.find({},{}).sort({u id:1}).limit(1)
对于myResult中的项目:
打印项目
我知道这是很久以前的事了,但我花了一些时间在上面浏览,找不到一个简单的解释
希望这有帮助。find
将返回一个游标,您必须迭代该游标以获得结果,至于获得0
的计数,我唯一能想到的是,对于特定的查询,它是0
Hi。谢谢你的评论。关于如何“迭代”光标,有什么建议吗?或者,你是否有一个链接,指向某个更详细地解释这一点的地方?谢谢<代码>用于tweets中的项目。使用新行查找({“screen_name”:“name”})
:打印项目
将迭代一个游标或向您询问答案。奇怪的是,当我在tweets中查询item.find({'lang':'en}):print(item),但当我在tweets中查询item.find({'screen_name':'name}):print(item)时,该命令不起作用。“screen_name”是否可能位于子文档中(如本页其他地方所建议的),如果是这样的话:我如何绕过它以便执行我的查询?再次感谢。您想查询屏幕名称,如:tweets.find({'user.screen\u name':'name')
Hi,谢谢您的回答。我已经尝试了您建议的两种方法,但是python不会像expexted那样为我返回tweet。这很奇怪,因为当我查询集合中的第一条tweet时,我会得到一个结果,但当我查询该tweet的特定属性时——即“screen_name”:“name”——我什么也得不到。你知道这里可能发生了什么吗?显然,在你的tweets集合中,screen_name字段没有“name”作为值的文档。这就是0的计数在示例代码中演示的内容。如果您可以向我们显示您要查找的文档,我们可以帮助您编写一个查询来查找它。=)现在,我已将整个文档添加到现有问题中。谢谢,这很有帮助。我的文档中有一个项目,所以我确信我没有子文档,但一直收到光标消息。在阅读了你提供的这个方法并使用它之后,它确实起到了作用。文档结构对于注释框来说太长了。有什么方法可以绕过这个问题,例如直接向您发送消息吗?@user2161725只需将其添加到问题中,并将其设置为代码块我现在已将整个文档添加到现有问题中。@user2161725我刚看到您的评论抱歉,如果你愿意的话,我们可以随时交谈,但我更希望你发布问题,并将其作为Sammaye提到的代码块,这样每个人都可以从答案中受益。我刚刚尝试了这种方法,它对我有效。我的问题是(这里的新手对我如此赤裸裸),每次我想在pymongo中看到我的结果时,我必须迭代我的收藏吗?或者有没有一种方法可以简单地查看结果,而不必使用for循环
?在我的代码中,我尝试了打印mycollection.find(),并收到了
,但当我尝试了您的方法时,它产生了我想要的结果,即可读文档。
tweets.find({"user.screen_name": "JoeCatanzarita"}) #for example.
myResult = db.find({}, {<!-- blah blah blah for the fields you want -->}).sort({"_id":1}).limit(1)
for item in myResult:
print item