[SERVER-11878] Results in incorrect order for sharded query with bounded sort on multi-key field Created: 27/Nov/13  Updated: 06/Dec/22  Resolved: 18/Apr/16

Status: Closed
Project: Core Server
Component/s: Querying, Sharding
Affects Version/s: 2.5.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: J Rassi 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
Related
related to SERVER-8146 sort by array element is incorrect un... Closed
is related to SERVER-13426 weird $sort behavior Closed
is related to SERVER-19394 Results in incorrect order for query ... Closed
is related to SERVER-19397 Results in incorrect order for comple... Closed
is related to SERVER-19402 Change semantics of sorting by array ... Closed
Assigned Teams:
Query
Operating System: ALL
Participants:

 Description   

Given a query with a predicate and sort over the same field, and given a document where this field is an array, it is expected that the sort key generated for the document will be the first element in the array that is within the bounds specified by the predicate (or last element in the array, if a descending sort is requested). The merge sort for sharded queries does not choose sort keys this way; the entire array is incorrectly chosen as the sort key.

Example: suppose that "foo" is a sharded collection over two shards, and each shard contains one document.

mongos> db.foo.find().sort({a:1})
{ "_id" : -1, "a" : [  3,  5 ] }
{ "_id" : 0, "a" : [  4 ] }
mongos> db.foo.find({a:{$gt:3}}).sort({a:1}) // results in wrong order
{ "_id" : -1, "a" : [  3,  5 ] }
{ "_id" : 0, "a" : [  4 ] }

The results for the second query above are in the incorrect order. The sort key for {_id: -1} should be "5", and the sort key for {_id:0} should be "4".

On "bar", an unsharded collection with the same data, the results appear in the correct order.

mongos> db.bar.find().sort({a:1})
{ "_id" : -1, "a" : [  3,  5 ] }
{ "_id" : 0, "a" : [  4 ] }
mongos> db.bar.find({a:{$gt:3}}).sort({a:1}) // results in correct order
{ "_id" : 0, "a" : [  4 ] }
{ "_id" : -1, "a" : [  3,  5 ] }



 Comments   
Comment by David Storch [ 18/Apr/16 ]

I can confirm that this was fixed by the introduction of {$meta: 'sortKey'} projection as part of the work in SERVER-15176. In particular, the code fix was checked in as a series of commits under SERVER-19355. The fix is available in all 3.2.x stable releases. Closing as a Duplicate.

Comment by J Rassi [ 31/Mar/16 ]

I suspect that this was resolved with the introduction of the {$meta: 'sortKey'} projection in SERVER-15176. Moving to "Needs Triage" in the query backlog for verification at our next triage meeting.

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