2015-01-08 3 views
0

저는 파이썬을 처음 사용합니다.파이썬 - dict 내부에 pymongo리스트

나는 무엇을 : 나는 MongoDB를하고 파이썬을 사용하고

. 고유 한 날짜를 사용하여 해당 날짜의 모든 주석을 검색하려고합니다.

예를 들어 datebase :

id|created_time|comment |..... 
0 |2014-01-01 | hi! |...... 
1 |2014-02-01 | hello! |...... 
2 |2014-01-01 | bye |......` 

내가 뭘하려 : '

text = db.comments.find(timeout=False) 
time = db.comments.distinct('created_time') 

#some code 
#get all distinct date and put into list 
timeArray = [] 
for allTime in time: 
    timeArray.append(allTime) 

comDict = {} 
for allCom in text: 
    global comDict 
    if(allCom['created_time'] == timeArray[1]): 

     #note i'm used timeArray[1] to test the code. 

     comDict.update({allCom['created_time']:allCom['comments']}) 

print comDict 

DICT 따라서 .update 킵이 그것을 추가하는 대신 값을 변경, 키를 복제 할 수 없습니다하지만 난 돈 다른 방법을 알지 못한다.

내가 필요한 :

{2014-01-01:['hi','bye'], 2014-02-01:['Hello!']} 

내가 위의 결과를 얻을 수 있도록 노력하겠습니다하지만 난 그렇게하는 방법을 모르겠습니다. 누군가 내가해야 할 일이나 내가 잘못한 일을 가르쳐 주실 수 있습니까?

미리 감사드립니다.

pipe = [ 
    {'$match': {'$timeout': False}}, 
    {'$group': {'_id': '$created_time', 
       'comments': {'$push': '$comments'}}}, 
] 
db.comments.aggregate(pipeline=pipe) 

답변

4

aggregate()를 사용하여 기록 그룹 MongoDB를하자, 파이썬 수준에 그것을 해결하지 마십시오
0

을 당신이 정말로 파이썬 사용하려면 :

dict0={} 
if not 'date' in dict0.keys(): dict0['date']=[value] 
else: dict0['date'].append(value)