Welcome to the MongoDB Community @juniorprogrammer!
JavaScript objects (and analogous data structures such as dictionaries and hashes in other programming language) do not guarantee ordering of keys. This is definitely a consideration when ordering can be significant in the BSON format used by the MongoDB server.
The mongo shell will preserve the order of alphanumeric keys in JavaScript objects, but numeric keys (or strings that look like numbers) will be re-ordered to the front of the object as per your example and the open issue you referenced. This behaviour is part of the JavaScript language implementation used by the 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).
The mongo shell (as at MongoDB 4.2) has a historical implementation of Map that differs from modern JavaScript, so this is a lingering issue that still needs to be resolved.
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.
Regards,
Stennie