Take the 2-minute tour ×

I have a Mongo find query that works well to extract specific fields from a large document like...

  { "profile.ModelID" : 'LZ241M4' },
    _id : 0,
    "profile.ModelID" : 1,
    "profile.AVersion" : 2,
    "profile.SVersion" : 3

...this produces the following output. Note how the SVersion comes before the AVersion in the document even though my projection asked for AVersion before SVersion.

{ "profile" : { "ModelID" : "LZ241M4", "SVersion" : "3.5", "AVersion" : "4.0.3" } }
{ "profile" : { "ModelID" : "LZ241M4", "SVersion" : "4.0", "AVersion" : "4.0.3" } }

...the problem is that I want the output to be...

{ "profile" : { "ModelID" : "LZ241M4", "AVersion" : "4.0.3", "SVersion" : "3.5" } }
{ "profile" : { "ModelID" : "LZ241M4", "AVersion" : "4.0.3", "SVersion" : "4.0" } }

What do I have to do get the Mongo JavaScript shell to present the results of my query in the field order that I specify?

share|improve this question
Where is your query? Have you considered using sort. –  jamjam Aug 15 '12 at 23:27
My query is up there. On one line its: db.profiles.find( { "profile.ModelID" : 'LZ241M4' }, { _id : 0, "profile.ModelID" : 1, "profile.AVersion" : 2, "profile.SVersion" : 3 } ); I haven't considered sort(), but that sorts the "rows", I am trying to sort the "columns". –  Bob Kuhar Aug 15 '12 at 23:33
Sorry, I don't know how I missed that. I think you already know but there is such thing as "columns" in mongodb. Is this your whole document or is profile a subset of larger document? –  jamjam Aug 16 '12 at 0:00
"profile" is a sub-document of a larger document. I put the quotes around "rows" and "columns" because I know that Mongo is all about documents and fields, but for the purpose of this conversation "fields" and "columns" are conceptually similar. My projection asked for the "fields" in the order { _id : 0, "profile.ModelID" : 1, "profile.AVersion" : 2, "profile.SVersion" : 3 } but the Mongo JavaScript shell does not produce the output for each document that way. I am searching for a way to rearrange this output per document. Thanks for your help –  Bob Kuhar Aug 16 '12 at 0:33

1 Answer 1

up vote 4 down vote accepted

I get it now. You want to return results ordered by "fields" rather the value of a fields.

Simple answer is that you can't do this. Maybe its possible with the new aggregation framework. But this seems overkill just to order fields.

The second object in a find query is for including or excluding returned fields not for ordering them.

    _id : 0,               // 0 means exclude this field from results
    "profile.ModelID" : 1, // 1 means include this field in the results
    "profile.AVersion" :2, // 2 means nothing
    "profile.SVersion" :3, // 3 means nothing

Last point, you shouldn't need to do this, who cares what order the fields come-back in. You application should be able to make use of the fields it needs regardless of the order the fields are in.

share|improve this answer
I don't share your opinion that its "overkill" to have the fields returned in the order the projection recommends, but its mongo; i've gotten used to disappointment. Thanks for your help. –  Bob Kuhar Aug 16 '12 at 15:33
So both the "ordered" and "unordered" version represent the same data. In SQL, the column order may be important for things like DataReader where you pick off a column by number. But with MongoDB you are effectively getting back a hash table. There's no ordering feature, because the key order of a hash table is not supposed to be relevant. However, I do like the proposal that the inclusion number could be an order. That's definitely worthy of a JIRA ticket. (jira.mongodb.org) –  Gates VP Aug 16 '12 at 16:13
I havnt tested this, but judging by the output @Bob got, the "2" and "3" did mean something: they were equivalent to using "1". ie: zero means don't include, non-zero means include. –  drevicko Dec 15 '12 at 2:47
From a practical standpoint, and purely as a personal preference, it would be helpful to be able to order the output fields when using the mongodb shell during development or debugging. I don't always want to see the _id field as the first output column when I do a find(). Yeah, I may want to see _id but I'm more interested in other fields first, so always seeing _id first when it's included can be considered noise. –  Inactivist May 5 '13 at 14:02
@GatesVP: an obvious use case of field order being relevant is displaying the results of find() in the mongo shell. I've filed a ticket for that - jira.mongodb.org/browse/SERVER-12599 –  Dan Dascalescu Feb 4 at 15:25

Your Answer


