Python + MongoDB - 使用新数据库查询Twitter消息时出错

时间:2015-09-29 11:58:46

标签: python mongodb twitter

我是Python + MongoDB的新手,目前正致力于从各种配置文件(twitter_profile)中搜索推文(twitter_message),这些配置文件作为我的硕士论文的一部分包含在列表中。

我有一个使用以下格式的数据库:

 "_id": ObjectId("557830f2b9ba7a2e7425d8db")
   "create_at": "null"
   "id": 1620930714
   "friendships": Array [0]
   "versions": Array [2]

我对这个数据库结构的查询是:

for profile in listProfiles:
     profile_id = db.mongo.get_one(collection='twitter_profile', where={'versions.screen_name':profile}, select={'id':True, '_id':0})        
        listProfilesId.append(profile_id['id'])    
    AllTweetsFromProfile = []    
    for iProfile in range(0,len(listProfilesId)):
        profile_id = listProfilesId[iProfile]
        tweets = db.mongo.get_all(collection='twitter_message', where={'profile':profile_id}, select={'text':True, '_id':0})      
        for tweet in tweets:
            AllTweetsFromProfile.append(tweet['text'])
        tweetsProfile[listProfile[iProfile]] = AllTweetsFromProfile

直到我为这样的事情改变了数据库结构,它才能正常工作:

    "_id": 61884410
       "_cls": TwitterProfile
       "versions": Array [2]
       "relationships": Array [0]
       "_alias": male

所以,请记住,在这个新的数据库中,我没有id字段,问题是:我应该对此查询做出哪些更改才能使其与我的新数据库一起使用?

我尝试仅使用_id,如下所示:

for profile in listProfiles:
     profile_id = db.mongo.get_one(collection='twitter_profile', where={'versions.screen_name':profile}, select={'_id':True})        
        listProfilesId.append(profile_id['_id'])  

但是,由于我收到错误,它并没有被证明是正确的:

  

TypeError:' NoneType'对象没有属性' getitem '

提前感谢所有寻求帮助和最好问候的人,

0 个答案:

没有答案