當我下面的文件存儲到蒙戈,是這樣的:用pongongo從mongo中檢索文檔時的排序屬性?
{
name: Somename,
profile: Someprofile
}
當我使用一個find_one():
我得到的是這樣一個結果:
{
profile: Someprofile,
_id: 35353432326532(random mongo id),
name: Somename
}
有python中的某種方式,使得當我在find_one之前或之後執行某些操作時,可以得到如下排序的json字符串的結果:
{
_id: 35353432326532(random mongo id),
name: Somename,
profile: Someprofile
}
我試過使用下面的OrderedDict,但它似乎沒有幫助。
somedocument = db.mycollection
theordereddict = OrderedDict(data_layer.find_one())
print str(theordereddict)
如何獲得我的輸出字符串以正確的順序關於屬性?在我甚至將文檔插入數據庫之前,這個命令是否由別的東西決定了?
什麼是你想要依靠在JSON文檔中字段的順序上的原因嗎?因爲他們被命名,你可以通過關鍵名稱引用他們,而不用擔心訂單... – 2012-08-14 21:09:45
不錯的閱讀:http://code.activestate.com/recipes/52306-to-sort-a-dictionary/ – 2012-08-14 21:49:00