[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:
Duplicate
duplicates SERVER-2991 Nice to have bson fields returned in ... Open
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:

db.links.find() output

{ "name" : "ABC", "links" : [     {       "url" : "http://www.abc.com/",      "text" : "Home page" } ] }
{ "name" : "DEF", "links" : [        {       "url" : "http://www.def.com",   "text" : "Home page" } ] }
{ "links" : [   {       "url" : "http://www.ghi.com",     "text" : "Home page" } ], "name" : "GHI" }
{ "links" : [   {       "url" : "http://www.jkl.com/",      "text" : "Home page" } ], "name" : "JKL" }
{ "name" : "MNO", "links" : [         {       "url" : "http://www.mno.com/",     "text" : "Home page" } ] }



 Comments   
Comment by Matt Kangas [ 21/Jul/14 ]

After inquiring about SERVER-11833, the short answer is: when a projection is present in a query

  • v2.4: result field order = document order, not projection order
  • v2.6: result field order = undefined

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 SERVER-11833, field order of results for queries with projection changed between 2.4 and 2.6. As of 2.6 it should the result field order should match the projection order. However, when I retry with 2.6.3 I see:

MongoDB shell version: 2.6.3
> db.version()
2.6.3
> db.order.drop()
false
> db.order.insert({_id:1, first:1, second:1})
WriteResult({ "nInserted" : 1 })
> db.order.findOne({}, {second:1, first:1})
{ "_id" : 1, "first" : 1, "second" : 1 }

This is not what SERVER-11833 predicts.

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:

https://code.google.com/p/v8/issues/detail?id=164

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:

> db.foo.find({...}, {a:1, b:1, c:1})
{a: "foo", b: "bar", c: "baz"}
 
> db.foo.find({...}, {c:1, b:1, a:1})
{c: "baz", b: "bar", a: "foo"}

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

4.3.3 Object
An object is a member of the type Object. It is an unordered collection of properties each of which
contains a primitive value, object, or function. A function stored in a property of an object is called a
method.

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,
Thomas

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

Generated at Thu Feb 08 03:29:00 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.