0

The cursor returned from pymongo query has result in different sequence then originally stored in database. why and how to fix it.

Returned Json

{"ID":"123", "Remediation":"restart", "credentials":"user:pass"}

Expected Json (I expect credentials first)

{"ID":"123", **"credentials":"user:pass"**, "Remediation":"restart"}

code

client=pymongo.MongoClient()   
db=client.Mydatabase   
cursor=db.paramtable.find()   
print dir(cursor)   
styvane
  • 59,869
  • 19
  • 150
  • 156
  • 1
    Python dict is unordered. While document in MongoDB is ordered. – gzc Jan 06 '17 at 15:54
  • At this point I only have one test element. {"ID":"123", "credentials":"user:pass", "Remediation":"restart"} and when I query this element on database the results are unchanged – Anup Singh Jan 06 '17 at 15:58
  • I want pymongo to return data as is, no changes – Anup Singh Jan 06 '17 at 16:00
  • The order I refered is keys order inside dict. In python, {"ID":"123", "credentials":"user:pass", "Remediation":"restart"} == {"ID":"123", "Remediation":"restart", "credentials":"user:pass"} – gzc Jan 06 '17 at 16:02
  • 1
    If you want to keep original order, use `as_class=OrderedDict` option. see this [question](http://stackoverflow.com/questions/23001156/how-to-get-ordered-dictionaries-in-pymongo) – gzc Jan 06 '17 at 16:08

0 Answers0