[DOCS-15307] Description of Sort Order for Arrays in Two Separate Pages Seem to Contradict Each Other Created: 03/May/22  Updated: 13/Nov/23  Resolved: 20/May/22

Status: Closed
Project: Documentation
Component/s: manual, Server
Affects Version/s: None
Fix Version/s: 5.0.0, 5.3.0, 6.0.0, Server_Docs_20231030, Server_Docs_20231106, Server_Docs_20231105, Server_Docs_20231113

Type: Task Priority: Minor - P4
Reporter: Ronan Merrick Assignee: Jason Price
Resolution: Done Votes: 0
Labels: needs-investigation
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 1 year, 37 weeks, 5 days ago
Epic Link: DOCSP-11701
Story Points: 3

 Description   

Hi,

On this page we describe the comparison order for different types. For array fields, we say the following:

With arrays, a less-than comparison or an ascending sort compares the smallest element of arrays, and a greater-than comparison or a descending sort compares the largest element of the arrays. 

On this page, we say in two places here and here that arrays are sorted according to the first element in the array.

These two pages seem to contradict each other. One seems to say that array fields are sorted by the smallest/largest element and the other seems to say that they are sorted according to the first element.

I've been doing some testing with it and the first page seems to be the correct one. For example:

(This was tested on v5.0.5)

db.foo.insertMany([
{a:[2,{'x':1}]},
{a:[{'x':3},1]}
])

If the sort is on the first element, I would expect the document with 2 as the first element to be sorted first because it's a number and this should be before the other document which has an Object in it's first element according to the comparison order.

However, the sort order is as follows:

> db.foo.find().sort({a:1})
{ "_id" : ObjectId("6271639f153ca114c0fd6d74"), "a" : [ { "x" : 3 }, 1 ] }
{ "_id" : ObjectId("6271639f153ca114c0fd6d73"), "a" : [ 2, { "x" : 1 } ] }

The document with ObjectId 6271639f153ca114c0fd6d74 is sorted first because it's smallest element is 1. If it was being sorted by the first element, it should be second.

This appears to match the way it is described in the Comparison/Sort Order page, but not in the Use Indexes to Sort Query Results page.



 Comments   
Comment by Githook User [ 20/May/22 ]

Author:

{'name': 'jason-price-mongodb', 'email': '69260375+jason-price-mongodb@users.noreply.github.com', 'username': 'jason-price-mongodb'}

Message: DOCS-15307 sort order arrays (#1107) (#1143)

Co-authored-by: jason-price-mongodb <jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com>

Co-authored-by: jason-price-mongodb <jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com>
Branch: v5.0
https://github.com/10gen/docs-mongodb-internal/commit/c24b25695d780312f46c7dd6d6043a932ee8de28

Comment by Githook User [ 20/May/22 ]

Author:

{'name': 'jason-price-mongodb', 'email': '69260375+jason-price-mongodb@users.noreply.github.com', 'username': 'jason-price-mongodb'}

Message: DOCS-15307 sort order arrays (#1107) (#1142)

Co-authored-by: jason-price-mongodb <jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com>

Co-authored-by: jason-price-mongodb <jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com>
Branch: v5.3
https://github.com/10gen/docs-mongodb-internal/commit/ab6087535fb5313451f9ffce66a537bd2346ec55

Comment by Githook User [ 19/May/22 ]

Author:

{'name': 'jason-price-mongodb', 'email': '69260375+jason-price-mongodb@users.noreply.github.com', 'username': 'jason-price-mongodb'}

Message: DOCS-15307 sort order arrays (#1107)

Co-authored-by: jason-price-mongodb <jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com>
Branch: master
https://github.com/10gen/docs-mongodb-internal/commit/a7083782c725af0ab3faf4d0261ca9180a8034bb

Generated at Thu Feb 08 08:12:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.