1
成功地將一個OrderedDict對象插入到MongoHQ後,我嘗試通過PyMongo的collection.find_one()命令查詢相同的OrderedDictionary。這失敗了,密鑰的順序丟失了。冠軍變成了一個普通的冠軍。在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')}
難道你們幫幫我嗎?非常感謝。
解決:db_conn = pymongo.Connection('的MongoDB:// *: * @ *。mongohq.com:*/testDatabase',document_class = collections.OrderedDict) – user1337