[DOCS-1111] Misleading/ambiguous statement in the index page regarding sort and query Created: 11/Feb/13  Updated: 22/Jan/14  Resolved: 29/Jul/13

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: v1.2

Type: Task Priority: Major - P3
Reporter: Kay Kim (Inactive) Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to DOCS-1521 Sort collections by a sub-element arr... Closed
Participants:
Days since reply: 10 years, 29 weeks, 2 days ago

 Description   

The Indexing Strategies page (http://docs.mongodb.org/manual/applications/indexes/#use-indexes-to-sort-query-results) has the following statement:

For queries that include a sort that uses a compound index, ensure that all fields before the first sorted field are equality matches.

I verified with Aaron to decode the sentence as such:

For queries that include a sort that uses a compound index, if the sortuses a subset of the index, such as .sort(

{ b: 1 }

), then to ensure
that MongoDB uses the index both to retrieve and sort, the query should use equality conditions for those index fields that precede the
sort index field.

So, for an index of

{ a: 1, b: 1, c:1, d: 1 }
  • If we have a query with .sort( { b: 1 }

    ), the preceding index field 'a' should be included in the query with an equality condition:

db.collection.find(

{ a: 5 }

).sort(

{b:1 }

)

  • If the query condition on the preceding index field is not an equality
    condition, MongoDB may sort the documents after it retrieves them
    from an index (i.e. the scanAndOrder: true)

db.collection.find( { a: {$gt: 5} } ).sort(

{ b:1 }

)

As for the query you were specifically wondering about:

db.collection.find(

{ b:5 }

).sort(

{ a:1, b:1 }

)

that query uses a subset in the .sort(); however, the first index field in the sort keys is 'a' and in our index of

{ a: 1, b: 1, c:1, d: 1 }

, there is no field
preceding 'a'; so it does not need to be part of the query condition.



 Comments   
Comment by auto [ 29/Jul/13 ]

Author:

{u'username': u'kay-kim', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-1111 clarify sort operations with index

DOCS-1111 incorporate some code review feedback

DOCS-1111 clarify sort operations with index

Signed-off-by: Sam Kleinman <samk@10gen.com>
Branch: master
https://github.com/mongodb/docs/commit/88294e2f7ab9691bb3703ae260f4c75141d38710

Comment by auto [ 29/Jul/13 ]

Author:

{u'username': u'kay-kim', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-1111 clarify sort operations with index

DOCS-1111 incorporate some code review feedback

DOCS-1111 clarify sort operations with index

Signed-off-by: Sam Kleinman <samk@10gen.com>
Branch: master
https://github.com/mongodb/docs/commit/88294e2f7ab9691bb3703ae260f4c75141d38710

Comment by auto [ 29/Jul/13 ]

Author:

{u'username': u'kay-kim', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-1111 clarify sort operations with index

DOCS-1111 incorporate some code review feedback

DOCS-1111 clarify sort operations with index

Signed-off-by: Sam Kleinman <samk@10gen.com>
Branch: master
https://github.com/mongodb/docs/commit/88294e2f7ab9691bb3703ae260f4c75141d38710

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