[SERVER-12599] When projection is present in a query, return the fields in the order of projection Created: 04/Feb/14 Updated: 06/Dec/22 Resolved: 22/Feb/18 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Querying |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Minor - P4 |
| Reporter: | Dan Dascalescu | Assignee: | Backlog - Query Team (Inactive) |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Assigned Teams: |
Query
|
||||||||
| Participants: | |||||||||
| Description |
|
I'm very well aware that mongo stores documents and the order of the keys doesn't matter; but find() results with fields that jump around are quite jarring:
|
| Comments |
| Comment by Matt Kangas [ 21/Jul/14 ] | |||||||||
|
After inquiring about
The order was deliberately left undefined. Partially for flexibility in implementation, partially because V8's order preservation is not entirely a guarantee (e.g. too complicated to explain and is subject to change). We aren't planning to revisit this decision soon but it is a reasonable feature request. Relabeling ticket appropriately. | |||||||||
| Comment by Dan Dascalescu [ 21/Jul/14 ] | |||||||||
|
Thomas, thanks for the explanation. If V8's strict ordering could be used, at least for the shell, that would be great. When I reported this issue, I was using Mongo 2.4.3. | |||||||||
| Comment by Matt Kangas [ 21/Jul/14 ] | |||||||||
|
Hi dandv, Which version of the server and client are you using? According to
This is not what Thomas is correct that ECMA-262 makes no guarantees about object field ordering, but V8 is more stringent than the spec. V8 does maintain object fields in insertion order, except for array indices (i.e., a property name that can be parsed as a 32-bit unsigned integer). Details here: | |||||||||
| Comment by Thomas Rueckstiess [ 20/Jun/14 ] | |||||||||
|
Hi Dan, I've made the title of the ticket reflect your last comment a little better. If I understand correctly (also considering the stack overflow post) what you're asking is: If a projection is given, return the fields ordered the same way as the projection. Example:
The issue is that many languages do not support field order for their map/object/dictionary types, including JavaScript, which we use in the shell. From the ECMAScript Language Specification
Therefore this feature request would be treated as cosmetic shell improvement (i.e. an implementation detail may cause the fields to be printed in the specified order) but cannot guarantee the field order once we convert from BSON to any of the driver languages (including the javascript shell). Please let us know if you have additional input or suggestions. Regards, | |||||||||
| Comment by Dan Dascalescu [ 04/Feb/14 ] | |||||||||
|
This is particularly annoying when projections are used, in which case the user has explicitly expressed an order of fields. See for example http://stackoverflow.com/questions/11976772/ordering-fields-from-find-query-with-projection |