[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: |
|
||||||||||||||||||||||||
| 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:
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 |