RT.
I have been doing data preprocessing recently. I want to use a list to store all the keys in the code, and then extract the field information of the corresponding entry by passing in the subscript of the specified column. It is best to fix the order of keys in the list, such as the order in which documents store keys in mongodb. Since find_one and other related methods return a dict type, they are out of order. I would like to ask if there is any way to extract all keys and store them in the order they are stored in the database?
It should be noted that the definition of key-value pairs in JSON is inherently unordered, and the database does not guarantee the order in which BSON objects will be stored. So don't rely on this order for anything.
Try this:
Set a key list and a tool for generating OrderDict;
Deposit into mongo normally;
Query the results and use the above "Tool to generate OrderDict" to generate an OrderDict instance object.
I don’t know if this can meet your needs. But if your object needs to be transmitted using methods such as JSON, the order may be messed up again.