[SERVER-28569] Shell APIs violate ECMAScript's object property order spec Created: 31/Mar/17  Updated: 06/Dec/22  Resolved: 03/Dec/21

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 3.5.5
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Philipp Tarasiewicz Assignee: Backlog - Server Tooling and Methods (STM) (Inactive)
Resolution: Won't Fix Votes: 0
Labels: move-stm
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to MONGOSH-1037 Numeric field are sorted to the front... Closed
Assigned Teams:
Server Tooling & Methods
Participants:

 Description   

If I want to add a sorting step into my aggregation pipeline which should sort the input documents according to two (or more) fields I can do so by adding a $sort stage (whose API is described here):

{ $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }

Given the current API the order in which the individual field sorting operations (sort by field1, then by field2, etc.) are applied is determined by the field order of the object that is assigned to the $sort property.
But this violates the ECMAScript spec...
E.g. ECMAScript 5 spec says that object properties are intrinsically unordered.
And ECMAScript 6 spec defines a specific object property order, but it does not equal to the order which the object properties have been defined in (see: https://esdiscuss.org/topic/nailing-object-property-order).

So it would be nice to have a $sort API that doesn't break the ECMAScript semantics, e.g. by using a strictly ordered data structure like an array:

{ $sort: [ [<field1>, <sort order>], [<field2>, <sort order>], ... ] }



 Comments   
Comment by Brooke Miller [ 03/Dec/21 ]

We've deprecated the mongo shell in favor of the new mongosh. Unfortunately, we aren't able to pursue improvements to the deprecated shell except in extreme cases, such as critical security fixes. Please start making use of mongosh and let us know if it works for you in this case.

Comment by Spencer Jackson [ 10/Apr/17 ]

justphil, thank you for reporting this issue. Your analysis is correct, and this would be a useful improvement to the shell's API. I'm going to place this ticket onto our backlog, so that we can schedule it in the future.

Comment by David Storch [ 31/Mar/17 ]

This is an issue not just for aggregation $sort but also in a number of other places (find's sort() helper and index key pattern ordering, to name a few). Since these JavaScript objects are serialized to BSON before being sent over the wire to the server, and since BSON is ordered, this is really an issue with the shell. Drivers must provide an ordered interface for specifying sorts. For example, pymongo's sort helper uses a python list rather than a python dictionary. The shell, however, does have a broken API in many cases where it expects a particular property ordering from a JS object.

Generated at Thu Feb 08 04:18:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.