Welcome to the MongoDB Community @juniorprogrammer!
mongo shell (currently the MozJS runtime).
Most languages provide an order-preserving data type which can be used as an alternative (for example,
Map in modern versions of Node.js). Official MongoDB drivers include a helper class if there isn’t a native data type, such as the
SON class in the Python driver).
mongo shell (as at MongoDB 4.2) has a historical implementation of
However, I strongly recommend avoiding numeric key names as this is both a straightforward workaround for this issue and avoids some syntactic ambiguity between array references and embedded fields. Using dot notation, a reference like
data.2 could either be referring to the 3rd element in the 0-based
data array or a field
2 embedded within
data . Using numeric field names may also result in unexpected outcomes (such as backfilling an array) with the right combination of update syntax and documents.