[SERVER-5672] Natural Sort ignores Index Created: 20/Apr/12  Updated: 05/Jan/14  Resolved: 20/Apr/12

Status: Closed
Project: Core Server
Component/s: Index Maintenance
Affects Version/s: 2.0.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Diego Maxi Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Capped Collection / 'rf' is sparse-indexed


Issue Links:
Related
related to SERVER-8607 add support for natural order sorting... Closed
Operating System: Linux
Participants:

 Description   

db.log.find(

{ rf : 'o-5556457634'}

).sort(

{ '$natural' : -1 }

).explain();
{
"cursor" : "ReverseCappedCursor",
"nscanned" : 1654468,
"nscannedObjects" : 1654468,
"n" : 4,
"millis" : 2932,
"nYields" : 5,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {}
}

Without the 'natural' sort:

db.log.find(

{ rf : 'o-5556457634'}

).explain();
{
"cursor" : "BtreeCursor rf_1",
"nscanned" : 4,
"nscannedObjects" : 4,
"n" : 4,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"rf" : [
[
"o-5556457634",
"o-5556457634"
]
]
}



 Comments   
Comment by Diego Maxi [ 24/Apr/12 ]

MongoDB seems to be a perfect solution to store large sets of application logs; and occasionally, I need to retrieve some of them (could be based on user_ip, user_id, log_type, etc). On those cases I need the resulting list to be sorted in reverse chronological order. Will give it a shot on the user group. Thanks!

Comment by Eliot Horowitz (Inactive) [ 24/Apr/12 ]

What are you trying to do exactly?
Do you really care about on disk order or are you trying to simulate something else?
This discussion is probably better suited for http://groups.google.com/group/mongodb-user/ as there are standard solutions, just want to make sure to give you the right one

Comment by Eliot Horowitz (Inactive) [ 24/Apr/12 ]

What are you trying to do exactly?
Do you really care about on disk order or are you trying to simulate something else?
This discussion is probably better suited for http://groups.google.com/group/mongodb-user/ as there are standard solutions, just want to make sure to give you the right one

Comment by Diego Maxi [ 23/Apr/12 ]

What about reverse natural sort on a query like this?
db.log.find({type: {$gte: 100}});
I'm trying understand what's the 'recommended' method to do (reverse) natural sorting on an application with dynamic querying. Do I need to modify the sort based on the query? Do I have to implement/index an auto-increment or microtime field? Thanks again!

Comment by Eliot Horowitz (Inactive) [ 21/Apr/12 ]

db.log.find(

{ rf : 'o-5556457634'}

).sort (

{ rf : -1 }

)

Comment by Diego Maxi [ 21/Apr/12 ]

The problem is that I need the results in reverse disk order. Do I have any way to accomplish this without creating additional fields? thanks!

Comment by Eliot Horowitz (Inactive) [ 21/Apr/12 ]

When you sort via an index, if the keys are dups, then the sort is disk order. So you don't need to do anything special.

Comment by Diego Maxi [ 21/Apr/12 ]

So the only way to accomplish this is by creating and indexing an additional custom field just for this task? Haven's seen any mention about this logic in the documentation and I thought the engine was able to look at the order on disk after getting the results (assuming the 'find' results will carry the disk order internally). Thanks.

Comment by Eliot Horowitz (Inactive) [ 20/Apr/12 ]

That's what $natural is supposed to do.
It forces a table scan to return them in the order they are on disk.
When doing a query, you shouldn't use $natural

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