Why does mongodb change the order of two insertions for the same set?

  mongodb, question

请输入图片描述

If you are referring to a change in the order of fields, it is because JSON itself defines Object as a collection of fields and is unordered. But MongoDB activelyThe order in which attributes are maintained.

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.

Why does the order of attributes change, because the implementation of collections is different in different languages. For example, dict in python does not maintain order, so the order has changed when it is passed to driver. if you want to maintain order, drivers provide ordered sets, such as those in python.SON.