[SERVER-5596] Array sort semantics Created: 13/Apr/12  Updated: 12/May/21  Resolved: 13/Apr/12

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Kyle Banker Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

We should clearly define the sort semantics for unindexed arrays. There's a discrepancy here:

> db.g.find().sort({a:-1})
{ "_id" : ObjectId("4f88436904495dab8a55071c"), "a" : [ 4 ] }
{ "_id" : ObjectId("4f88436804495dab8a55071b"), "a" : [ 3 ] }
{ "_id" : ObjectId("4f88436704495dab8a55071a"), "a" : [ 2 ] }
{ "_id" : ObjectId("4f88436504495dab8a550719"), "a" : [ 1 ] }
{ "_id" : ObjectId("4f88438604495dab8a55071d"), "a" : [ 1, 1 ] }
{ "_id" : ObjectId("4f88439c04495dab8a55071e"), "a" : [ 1, 1, 0 ] }
{ "_id" : ObjectId("4f88440204495dab8a55071f"), "a" : [ 1, 1, 0, 1 ] }
{ "_id" : ObjectId("4f88441104495dab8a550720"), "a" : [ 1, 1, 1, 1 ] }
 
> db.g.find().sort({a:1})
{ "_id" : ObjectId("4f88439c04495dab8a55071e"), "a" : [ 1, 1, 0 ] }
{ "_id" : ObjectId("4f88440204495dab8a55071f"), "a" : [ 1, 1, 0, 1 ] }
{ "_id" : ObjectId("4f88436504495dab8a550719"), "a" : [ 1 ] }
{ "_id" : ObjectId("4f88438604495dab8a55071d"), "a" : [ 1, 1 ] }
{ "_id" : ObjectId("4f88441104495dab8a550720"), "a" : [ 1, 1, 1, 1 ] }
{ "_id" : ObjectId("4f88436704495dab8a55071a"), "a" : [ 2 ] }
{ "_id" : ObjectId("4f88436804495dab8a55071b"), "a" : [ 3 ] }
{ "_id" : ObjectId("4f88436904495dab8a55071c"), "a" : [ 4 ] }

It seems that the reverse sort should mirror the forward sort, but it doesn't.



 Comments   
Comment by Eliot Horowitz (Inactive) [ 13/Apr/12 ]

Both seem to follow rule to me...
Come by and I'll explain in person.,

Comment by Kyle Banker [ 13/Apr/12 ]

Here's the sort with and index:

> db.g.ensureIndex({a: 1})
> db.g.find().sort({a:-1})
{ "_id" : ObjectId("4f88436904495dab8a55071c"), "a" : [ 4 ] }
{ "_id" : ObjectId("4f88436804495dab8a55071b"), "a" : [ 3 ] }
{ "_id" : ObjectId("4f88436704495dab8a55071a"), "a" : [ 2 ] }
{ "_id" : ObjectId("4f88441104495dab8a550720"), "a" : [ 1, 1, 1, 1 ] }
{ "_id" : ObjectId("4f88440204495dab8a55071f"), "a" : [ 1, 1, 0, 1 ] }
{ "_id" : ObjectId("4f88439c04495dab8a55071e"), "a" : [ 1, 1, 0 ] }
{ "_id" : ObjectId("4f88438604495dab8a55071d"), "a" : [ 1, 1 ] }
{ "_id" : ObjectId("4f88436504495dab8a550719"), "a" : [ 1 ] }
 
> db.g.find().sort({a:1})
{ "_id" : ObjectId("4f88439c04495dab8a55071e"), "a" : [ 1, 1, 0 ] }
{ "_id" : ObjectId("4f88440204495dab8a55071f"), "a" : [ 1, 1, 0, 1 ] }
{ "_id" : ObjectId("4f88436504495dab8a550719"), "a" : [ 1 ] }
{ "_id" : ObjectId("4f88438604495dab8a55071d"), "a" : [ 1, 1 ] }
{ "_id" : ObjectId("4f88441104495dab8a550720"), "a" : [ 1, 1, 1, 1 ] }
{ "_id" : ObjectId("4f88436704495dab8a55071a"), "a" : [ 2 ] }
{ "_id" : ObjectId("4f88436804495dab8a55071b"), "a" : [ 3 ] }
{ "_id" : ObjectId("4f88436904495dab8a55071c"), "a" : [ 4 ] }

Neither of these examples follows that rule. In both index and unindexed descending sorts, the arrays containing a '0' are not the final arrays to appear.

Comment by Eliot Horowitz (Inactive) [ 13/Apr/12 ]

This looks correct to me.

sort of an array is based on the min or max value, so it might not be consistent when reversing.

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