5

我在 mongodb 数据库中消费了一堆推文。我想使用 pymongo 查询这些推文。例如,我想查询 screen_name。但是,当我尝试这样做时,python 不会返回推文,而是返回有关 pymongo.cursor.Cursor 的消息。这是我的代码:

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}}

但是,当我尝试查询此 screen_name 时,我得到:

tweets.find({"screen_name": "JoeCatanzarita"})
<pymongo.cursor.Cursor at 0x52c02f0>

然后,当我尝试计算具有“screen_name”:“name”的推文数量时,我得到:

tweets.find({"screen_name": "name"}).count()
0

知道我做错了什么/如何让 pymongo 返回我正在寻找的推文吗?

谢谢!

4

4 回答 4

9

PyMongo 的 find() 方法返回一个 Cursor。要在服务器上实际执行查询并检索结果,请使用listfor 循环或 for 循环迭代游标:

for doc in tweets.find({'screen_name': 'name'}):
    print(doc)

# Or:
docs = list(tweets.find({'screen_name': 'name'}))

如果tweets.find({"screen_name": "name"}).count()返回 0,则表示没有文档与您的查询匹配。

编辑:既然您已经发布了一个示例文档,我看到您想查询如下:

list(tweets.find({'user.screen_name': 'name'}))

...因为该screen_name字段嵌入在user子文档中。

于 2013-07-05T17:58:38.587 回答
2

我认为问题在于“screen_name”在子文档中,如果您可以提供我可以帮助您的文档结构。

于 2013-07-05T17:39:20.640 回答
1

好的,现在我明白你的问题是什么了:

如果您仔细查看您的文档,您会注意到“screen_name”在子文档用户内部,因此如果您想访问它,您只需执行以下操作:

tweets.find({"user.screen_name": "JoeCatanzarita"})  #for example.

每当您处于尝试查找的元素位于子文档中的情况下,例如在这种情况下或在数组中时,请始终使用此语法。

于 2013-07-15T13:03:27.657 回答
1

我在 collection.find() 调用中遇到了同样的问题。

我检查了对象的类型,它是 python dict。因此,即使只有一个项目而且她的工作就像魅力一样,我还是接受了字典并对其进行了迭代。

myResult = db.find({}, {<!-- blah blah blah for the fields you want -->}).sort({"_id":1}).limit(1)
for item in myResult:
    print item

我知道这是很久以前的事了,但我花了一些时间浏览这个并找不到简单的解释。

希望这可以帮助。

于 2015-09-19T05:37:35.423 回答