开发者社区 问答 正文

mongodb为什么插入同样的集合顺序有变化

如图:同样的集合插入两次顺序不一样为什么
screenshot

展开
收起
蛮大人123 2016-02-14 15:32:50 2975 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    如果你指的是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.

    2019-07-17 18:42:39
    赞同 展开评论