How can you specify the order of properties in a javascript object for a MongoDB index in node.js? How can you specify the order of properties in a javascript object for a MongoDB index in node.js? mongodb mongodb

How can you specify the order of properties in a javascript object for a MongoDB index in node.js?


In MongoDB, the order of fields in a document is indeed significant, and all language drivers provide a means of specifying documents that way, even if the underlying programming language does not have such a concept.

The document format that MongoDB uses in its shell is JSON-like but not strict JSON. Among other things, order of fields is always preserved.

In Javascript, the standard defines fields as unordered, so implementations are free to ignore/not preserve the ordering. But in practice, all implementations do preserve the ordering. In particular the V8 engine preserves the ordering, which is the engine used in node.js so it's no problem.