MongoDB preserves the order of the document fields following write operations except for the following cases:
The _id field is always the first field in the document.
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 inorder of the fields int document.
如果你指的是fields的順序有變化,那是因為JSON本身定義Object是一個fields的集合,是無序的。但是MongoDB主動地維護屬性的順序。
那屬性的順序為什麼會變呢,這是因為各個語言中對集合的實作不一樣。例如python中的dict不維護順序,所以他傳到driver中的時候順序已經變了,如果希望保持順序,driver都提供了有序集合,比如python裡的 SON.