使用新数据库查询twitter消息时发生Python+MongoDB错误

2024-09-28 20:54:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python+MongoDB的新手,目前正在进行一个查询,从各种配置文件(twitter\u profile)中搜索tweets(twitter\u message),这些配置文件作为硕士论文的一部分包含在一个列表中。你知道吗

我有一个DB,它使用以下格式:

 "_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

它工作得很好,直到我把DB结构改成这样:

    "_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' object has no attribute 'getitem'

提前感谢大家的帮助和问候


Tags: inid数据库fordbgetmongotwitter