[DOCS-4428] Queries executed via MongoS will always project the sort key Created: 25/Nov/14  Updated: 30/Oct/23  Resolved: 21/May/19

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: Server_Docs_20231030

Type: Task Priority: Minor - P4
Reporter: David Hows Assignee: Andrew Aldridge
Resolution: Done Votes: 0
Labels: query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-16270 Set query filtering with mongoS/ per ... Backlog
Duplicate
duplicates DOCS-12652 [SERVER] Note addition of $sortKey me... Closed
Related
is related to SERVER-13008 Encoding of projection for query plan... Closed
Participants:
Days since reply: 7 years, 49 weeks, 2 days ago

 Description   

This is a "gotcha" when using index filters and evaluating query performance.

When you issue a query via a MongoS that includes both a Projection and a Sort field, the sort field will always be included within the projection set to the shards. This is needed for sorting on the result data from shards by desired field on the MongoS.

This means when you are adding index filters using projections, you should always expect to add the sort field to the projection in sharded environments.



 Comments   
Comment by David Storch [ 07/Mar/16 ]

This behavior is no longer present in mongos as of 3.2. As part of find/getMore commands project (SERVER-15176), we rewrote the find path on mongos. This rewrite changed how mongos obtains sort keys for performing its sorted merge.

Consider the following example query:

db.coll.find({}, {_id: 0, b: 1}).sort({a: 1});

This query both projects out a and sorts by it. Past versions of mongos would amend the projection to {_id: 0, b: 1, a: 1} so that the sort keys are returned to mongos in order to perform the sorted merge. 3.2.x and newer versions of mongos instead use something called sortKey $meta projection. In these newer versions, mongos will amend the projection to something like {_id: 0, b: 1, $sortKey: {$meta: 'sortKey'}}. This will cause the shards to exclude the a field but add a new field called "$sortKey" which contains the internally-generated sort key for each document. The sorted merge proceeds on mongos using this "$sortKey" information.

Comment by Charlie Page [ 03/May/15 ]

Not this simple. Query filters are not type insensitive, and they should be. Therefore the query filters have to match the type that mognoS uses (which probably isn't what users are using...). I linked SERVER-13008. I don't recall the type mongoS uses, but { a : 1 } doesn't match.

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