如图:同样的集合插入两次顺序不一样为什么
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
如果你指的是fields的顺序有变化,那是因为JSON本身定义Object是一个fields的集合,是无序的。但是MongoDB主动地维护属性的顺序。
参见:https://docs.mongodb.org/manual/core/document/#document-field-order.
MongoDB preserves the order of the document fields following write operations except for the following cases:
1.The _id field is always the first field in the document.
2.Updates that include renaming of field names may result in the reordering of fields in the document.
Changed in version 2.6: Starting in version 2.6, MongoDB actively attempts to preserve the field order in a document. Before version 2.6, MongoDB did not actively preserve the order of the fields in a document.
那属性的顺序为什么会变呢,这是因为各个语言中对集合的实现不一样。比如python中的dict不维护顺序,所以他传到driver中的时候顺序已经变了,如果希望保持顺序,driver都提供了有序集合,比如python里的 SON.