[DOCS-2179] .find().sort( { $natural: 1 } ) disables index use Created: 30/Oct/13  Updated: 16/Mar/15  Resolved: 04/Sep/14

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

Type: Improvement Priority: Major - P3
Reporter: Andrew Ryder (Inactive) Assignee: Tim Slavin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 9 years, 22 weeks, 5 days ago

 Description   

Regarding this page:
http://docs.mongodb.org/manual/reference/method/cursor.sort/

This contains a neat description of the use of .sort() specifically after a .find() and has a section at the bottom covering the special $natural operator.

The page should make it clear that the use of $natural effectively disables the use of most indexes.

There are some cases, particularly involving _id where $natural seems to work but these cases appear to be side-effects rather than intentional design choices.



 Comments   
Comment by Githook User [ 12/Sep/14 ]

Author:

{u'username': u'tychoish', u'name': u'Sam Kleinman', u'email': u'samk@10gen.com'}

Message: DOCS-2179: edit
Branch: master
https://github.com/mongodb/docs/commit/eda4eda9f9153cfcf2a14e7b5e64e9d984592c02

Comment by Githook User [ 12/Sep/14 ]

Author:

{u'username': u'TimSlavinMongoDB', u'name': u'Tim Slavin (MongoDB)', u'email': u'tim.slavin@10gen.com'}

Message: DOCS-2179: disables index use

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

Comment by Andrew Ryder (Inactive) [ 31/Oct/13 ]

Scenario

  • An extra index was created as {normal:1}
  • A series of relevant objects were inserted containing both _id and normal member values, for example, the 'string' type object was;

    db.coll.insert( { _id: "01", normal: "01" } )

  • Each distinct object used a different type for the member values, but the values within each object were the same
  • All operations were .find().sort().explain()

Results

.sort({...}) ->
.find({...}) V
$natural:1 _id:1 normal:1
_id:ObjectId()
_id:object()
_id:string()
_id:integer()
_id:BinData()
normal:(anything)

= explain() used a B-Tree cursor
= explain() scanned the entire collection

I think the official answer is the first column is not supported. And mileage may vary.

Comment by Andrew Ryder (Inactive) [ 30/Oct/13 ]

SERVER-5672 makes it clear that $natural disabling index use is "as designed" and can be documented to reflect that situation.

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