[SERVER-10831] Sorting doesn't work if sort key excluded in projection with a sharded collections Created: 20/Sep/13  Updated: 24/Nov/15  Resolved: 24/Nov/15

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

Type: Bug Priority: Major - P3
Reporter: Jeff lee Assignee: Unassigned
Resolution: Duplicate Votes: 4
Labels: query_triage
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File server10831.js    
Issue Links:
Related
related to SERVER-19355 Support skip, limit, and projection i... Closed
is related to SERVER-11877 mongos query results include sort fie... Closed
is related to DOCS-1978 Add sharding restriction: sort fields... Closed
Operating System: ALL
Steps To Reproduce:

1. Create a sharded cluster - 1 shard
2. Add a document to a collection. Does not require that the collection be sharded
3. Run a query against the collection, excluding the key in the projection while attempting to sort by the same key

Participants:

 Description   

Hi,

It looks like there was a behavior change between 2.0 and 2.2 that results in an error 13431 if you attempt to sort by a key that has been explicitly excluded in a projection when running a query via a mongos.

I was just wondering if this is expected behavior.

mongos> db.version()
2.0.9
mongos> db.foo.save({id:3, value:'c'})
mongos> db.foo.save({id:2, value:'b'})
mongos> db.foo.save({id:1, value:'a'})
mongos> db.foo.find({},{id:0}).sort({id:1})
{ "_id" : ObjectId("523bd35dff9a7c468936efba"), "value" : "a" }
{ "_id" : ObjectId("523bd358ff9a7c468936efb9"), "value" : "b" }
{ "_id" : ObjectId("523bd352ff9a7c468936efb8"), "value" : "c" }

mongos> db.version()
2.2.6
mongos> db.foo.find({},{id:0}).sort({id:1})
error: {
        "$err" : "have to have sort key in projection and removing it",
        "code" : 13431
}



 Comments   
Comment by J Rassi [ 24/Nov/15 ]

This has been fixed with the rewrite of the mongos query path. As of SERVER-19355, mongos now uses the "sortKey" $meta projection in order to retrieve the sort key for each document. As such, mongos no longer relies on the user projection properly preserving the field to be sorted.

Closing as a dup of SERVER-19355.

Comment by Ken Williams [ 17/May/15 ]

Regardless of whether this bug gets fixed or declared expected behavior, the error message could at least be changed to be more grammatical. I can't tell what it actually means.

Comment by pan shangbin [ 30/Nov/13 ]

i have also find this bug in version 2.4.8
mongos> db.scenic.find({},

{"_id":0}

).limit(5).sort(

{"_id":-1}

)
error: {
"$err" : "have to have sort key in projection and removing it",
"code" : 13431
}
mongos>

Comment by Eliot Horowitz (Inactive) [ 14/Oct/13 ]

This is indeed a bug and not "works as designed" not should it be a limitation. Mongos should handle transparently.

Comment by Scott Hernandez (Inactive) [ 20/Sep/13 ]

This is expected behavior for queries via mongos since they requires the data to do the merge sort. We can add this to the sharding restrictions page for future reference.

Also, this code has not changed since before 2.0: https://github.com/mongodb/mongo/blob/v2.0/client/parallel.cpp#L396
However I believe at some point we started using the parallel cursor for queries even if only one shard is selected (or if the collection is unsharded), for consistency reasons (along with other reasons).

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