2013-07-22 24 views
0

我有一個名爲「tweets」的集合存儲在我的mongodb數據庫中,名爲「test」。 我連接到數據庫以下列方式:如何使用pymongo查詢給定變量(包含hashtags的推文)的推文集合?

import sys 
import pymongo 
import Connection from pymongo 
connection = Connection() 
db = connection.test 
tweets = db.tweets 

我從我的微博作爲一個列表理解一個文檔:

list(tweets.find())[0] 

這說明我的文檔結構如下:

{u'_id': ObjectId('...'), 
u'contributors': None, 
u'coordinates': {u'coordinates': [-placeholder coordinate, placeholder coordinate], 
    u'type': u'Point'}, 
u'created_at': u'Mon Jun 24 17:53:47 +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': [40.81019674, -73.99020695], u'type': u'Point'}, 
u'id': 349223842700472320L, 
u'id_str': u'349223842700472320', 
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': [[[placeholder coordinate, placeholder coordinate], 
    [-placeholder coordinate, placeholder coordinate], 
    [-placeholder coordinate, placeholder coordinate], 
    [-placeholder coordinate, placeholder coordinate]]], 
    u'type': u'Polygon'}, 
    u'country': u'placeholder country', 
    u'country_code': u'example', 
    u'full_name': u'name, xx', 
    u'id': u'user id', 
    u'name': u'name', 
    u'place_type': u'city', 
    u'url': u'http://api.twitter.com/1/geo/id/1820d77fb3f65055.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'example text', 
u'truncated': False, 
u'user': {u'contributors_enabled': False, 
    u'created_at': u'Sat Jan 22 13:42:59 +0000 2011', 
    u'default_profile': False, 
    u'default_profile_image': False, 
    u'description': u'example description', 
    u'favourites_count': 100, 
    u'follow_request_sent': None, 
    u'followers_count': 100, 
    u'following': None, 
    u'friends_count': 100, 
    u'geo_enabled': True, 
    u'id': placeholder_id, 
    u'id_str': u'placeholder_id', 
    u'is_translator': False, 
    u'lang': u'en', 
    u'listed_count': 0, 
    u'location': u'example place', 
    u'name': u'example name', 
    u'notifications': None, 
    u'profile_background_color': u'000000', 
    u'profile_background_image_url': u'http://a0.twimg.com/images/themes/theme19/bg.gif', 
    u'profile_background_image_url_https': u'https://si0.twimg.com/images/themes/theme19/bg.gif', 
    u'profile_background_tile': False, 
    u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/241527685/1363314054', 
    u'profile_image_url': u'http://a0.twimg.com/profile_images/378800000038841219/8a71d0776da0c48dcc4ef6fee9f78880_normal.jpeg', 
    u'profile_image_url_https':  u'https://si0.twimg.com/profile_images/378800000038841219/8a71d0776da0c48dcc4ef6fee9f78880_normal.jpeg', 
    u'profile_link_color': u'000000', 
    u'profile_sidebar_border_color': u'FFFFFF', 
    u'profile_sidebar_fill_color': u'000000', 
    u'profile_text_color': u'000000', 
    u'profile_use_background_image': False, 
    u'protected': False, 
    u'screen_name': placeholder screen_name', 
    u'statuses_count': xxxx, 
    u'time_zone': u'placeholder time_zone', 
    u'url': None, 
    u'utc_offset': -21600, 
    u'verified': False}} 

然後我查詢與我的收藏主題標籤的所有文件:

list(tweets.find({'entities.hashtags.text': {"$ne":None}})) 

到目前爲止好。現在,這是我的問題。我想通過screen_name對我的集合中的文檔進行排序。我嘗試:

users = tweets.find({'entities.hashtags.text': {"$ne":None}}, {"user.screen_name":1}) 
for user in users: 
    print user.get["user.screen_name"] 

但收到以下錯誤信息:

TypeError         Traceback (most recent call last) 
/Users/home/<ipython-input-98-ea29cbbcfe27> in <module>() 
     1 for user in users: 
----> 2  print user.get["screen_name"] 
     3 

TypeError: 'builtin_function_or_method' object has no attribute '__getitem__' 

任何想法,我做錯了什麼,這裏/任何想法我怎麼能修復我的代碼?

謝謝!

回答

2

您使用括號與get方法,你應該使用小括號,所以無論是與user.get('screen_name')user['screen_name']訪問密鑰。

+0

This works。謝謝您的回答! – user2161725

相關問題