RT.最近在做数据预处理方面的东西,想在代码里用一个list来存放所有的key,然后通过传入指定column的下标来提取相应条目的字段信息。最好list中key的顺序固定,比如mongodb中document存储key的顺序。由于find_one等相关方法返回的是dict类型,所以是乱序,想问下有什么方法提取所有的key并按其在数据库中存储的顺序存放?
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.
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.