2014-01-06 2 views
1

OrderedDict 개체를 MongoHQ에 성공적으로 삽입 한 후 PyMongo의 collection.find_one() 명령으로 동일한 OrderedDictionary를 쿼리하려고합니다. 이것이 실패하면 키 순서가 손실됩니다. dictonary는 일반 dictonary가됩니다.MongoHQ의 OrderedDict 삽입 및 쿼리

내 코드는 다음과 같습니다

import collections 
import pymongo 

test = collections.OrderedDict() 
test.update({'test1': 1}) 
test.update({'test2': 2}) 
test.update({'test3': 3}) 
test.update({'test4': 4}) 
test.update({'test5': 5}) 
test 
>>>OrderedDict([('test1', 1), ('test2', 2), ('test3', 3), ('test4', 4), ('test5', 5 
)]) 

db_conn = pymongo.Connection('mongodb://*:*@*.mongohq.com:*/testDatabase') 
db_conn.testDatabase['testCollection'].insert(test) 
test_new = db_conn.testDatabase['testCollection'].find_one() 

print test_new 
>>> {u'test1': 1, u'test3': 3, u'test2': 2, u'test5': 5, u'test4': 4, u'_id': Object 
Id('52cc777b92c49c146cb5e3db')} 

너희들이 나를 도울 수 있을까요? 고마워.

+0

해결 :. db_conn = pymongo.Connection ('MongoDB를 : // * : * @ *. mongohq.com:*/testDatabase ', document_class = collections.OrderedDict) – user1337

답변

0

어쩌면 당신이 시도 할

test_new = db_conn.testDatabase [ '을 TestCollection'] find_one (as_class = collections.OrderedDict)