首页 > mongodb为什么同样的集合,两次插入的顺序会有变化呢?

mongodb为什么同样的集合,两次插入的顺序会有变化呢?


如果你指的是fields的顺序有变化,那是因为JSON本身定义Object是一个fields的集合,是无序的。但是MongoDB主动地维护属性的顺序。

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.

【热门文章】
【热门文章】