[DOCS-2760] Document sort order for array fields Created: 20/Feb/14  Updated: 11/Mar/14  Resolved: 11/Mar/14

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: v1.3.2

Type: Bug Priority: Major - P3
Reporter: A. Jesse Jiryu Davis Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File docs2760.js    
Issue Links:
Related
is related to SERVER-11444 $lt/$lte/$gte/$gt behaves differently... Closed
Participants:
Days since reply: 9 years, 49 weeks, 1 day ago

 Description   

If a collection contains:

{}
{a: 1}
{a: []}
{a: [1]}
{a: [0, 1]}

... there are some bizarre semantics for how it's sorted. The empty array comes before the absence-of-a-field, and "1" comes between [0, 1] and [1]:

> db.collection.find({}, {_id: false}).sort({a: 1})
{ "a" : [ ] }
{  }
{ "a" : [  0,  1 ] }
{ "a" : 1 }
{ "a" : [  1 ] }

Reversing the sort does not quite reverse the order of results:

> db.collection.find({}, {_id: false}).sort({a: -1})
{ "a" : 1 }
{ "a" : [  1 ] }
{ "a" : [  0,  1 ] }
{  }
{ "a" : [ ] }

There are a number of tickets, some incomplete, regarding this confusing behavior:



 Comments   
Comment by Githook User [ 11/Mar/14 ]

Author:

{u'username': u'kay-kim', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-2760 array sort order
Branch: v2.4
https://github.com/mongodb/docs/commit/a6fa9af41c9c3b0ef5e9536333b65496fde1c667

Comment by Githook User [ 11/Mar/14 ]

Author:

{u'username': u'kay-kim', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-2760 array sort order
Branch: master
https://github.com/mongodb/docs/commit/eb687375177cd8bf7d38b0e02548a2a0a4f25046

Generated at Thu Feb 08 07:44:11 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.