search

Home  >  Q&A  >  body text

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

天蓬老师天蓬老师2800 days ago512

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-24 09:14:02

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

    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? This is because the implementation of collections in each language is different. For example, dict in python does not maintain the order, so the order has changed when it is passed to the driver. If you want to maintain the order, the driver provides an ordered collection, such as SON in python.

    reply
    0
  • Cancelreply