0

I am trying to create the following document in the mongo db but for some reason the document saving the issue object before the data (i presume the default order is by key length). i would like that the issues object will be in the end of the document.

i am using PyMongo from flask_pymongo, i also try the following solution: https://stackoverflow.com/a/30787769/5494424

I try to work with bson and OrderedDict but without success

this is how i create the dict code in python:

 gold_apple_report = {
  "_id": "",
  "updated_on": "",      
  "data": {
    "sms": {
      "status": "todo"
    },
    "catchapp": {
      "status": "todo"
    },
    "telegram": {
      "status": "todo"
    },
    "bookmarks": {
      "status": "todo"
    },
    "calendar": {
      "status": "todo"
    },
    "call_history": {
      "status": "todo"
    },
    "contacts": {
      "status": "todo"
    },
    "files": {
      "status": "todo"
    },
    "downloads": {
      "status": "todo"
    },
    "notes": {
      "status": "todo"
    },
    "posts": {
      "status": "todo"
    },
    "app_list": {
      "status": "todo"
    },
    "media": {
      "status": "todo"
    },
    "locations": {
      "status": "todo"
    },
    "chats": {
      "whatsapp": {
        "status": "todo"
      },
      "telegram": {
        "status": "todo"
      },
      "skype": {
        "status": "todo"
      },
      "line": {
        "status": "todo"
      },
      "viber": {
        "status": "todo"
      },
      "facebook": {
        "status": "todo"
      },
      "twitter": {
        "status": "todo"
      },
      "instagram": {
        "status": "todo"
      }
    }
  },
  "issues": {

  }
}

and this is how it appear in the mongoDb:

 gold_apple_report = {
      "_id": "",
      "updated_on": "",
      "issues": {

      },      
      "data": {
        "sms": {
          "status": "todo"
        },
        "catchapp": {
          "status": "todo"
        },
        "telegram": {
          "status": "todo"
        },
        "bookmarks": {
          "status": "todo"
        },
        "calendar": {
          "status": "todo"
        },
        "call_history": {
          "status": "todo"
        },
        "contacts": {
          "status": "todo"
        },
        "files": {
          "status": "todo"
        },
        "downloads": {
          "status": "todo"
        },
        "notes": {
          "status": "todo"
        },
        "posts": {
          "status": "todo"
        },
        "app_list": {
          "status": "todo"
        },
        "media": {
          "status": "todo"
        },
        "locations": {
          "status": "todo"
        },
        "chats": {
          "whatsapp": {
            "status": "todo"
          },
          "telegram": {
            "status": "todo"
          },
          "skype": {
            "status": "todo"
          },
          "line": {
            "status": "todo"
          },
          "viber": {
            "status": "todo"
          },
          "facebook": {
            "status": "todo"
          },
          "twitter": {
            "status": "todo"
          },
          "instagram": {
            "status": "todo"
          }
        }
      }

    }
Matan Tubul
  • 774
  • 3
  • 11
  • 33
  • i already try that solution without a success – Matan Tubul Nov 29 '17 at 15:20
  • When you create the dict code in Python, it is already represented in an arbitrary order in memory - even if later the mongo driver will copy that data to an ordered dict. You have to create - in a proper way, passing a sequence of tuples - your data as an OrderedDict in your code, and then pass it to the mongodrivers (instantiated in a way to use OrderedDict, as in the linked answer). – jsbueno Nov 29 '17 at 15:34
  • @jsbueno thank you very much, is make sense what you say. i will try that – Matan Tubul Nov 29 '17 at 15:36
  • when i use the following code i still get the same order d = OrderedDict(gold_apple_report) – Matan Tubul Nov 29 '17 at 15:45
  • 2
    So, if you read my comment above, there is a part about "creating the OrderedDict in a proper way, passign a sequence of tuples". I won't give you a recipe for that in a comment - it would be nice if you searched around , found some examples, and understand what is going on when you create an OrderedDict. – jsbueno Nov 29 '17 at 15:54

0 Answers0