- 
    Type:
Question
 - 
    Resolution: Duplicate
 - 
    Priority:
Major - P3
 - 
    None
 - 
    Affects Version/s: 2.4.6
 - 
    Component/s: Geo, Index Maintenance
 
- 
        None
 
- 
        None
 - 
        None
 - 
        None
 - 
        None
 - 
        None
 - 
        None
 
I have a collection called search2 with about 20000 documents like this:
    {
        "loc": 
,
        "docId": 1,
        "docVote": 0,
        "title": "title-1",
        "_id": 
    }
I define an index with this command:
db.search2.ensureIndex(
{"docVote": 1,"loc":"2dsphere"});
On the collection there are only this index and the default index on "_id" field.
When i execute the following query i expect "nscannedObjects" to be = 10 :
    db.search2.find({
        loc: {
        $geoIntersects: {
            $geometry: 
        }
        }
    }, 
).sort(
{ "docVote": 1 }).limit(10).hint(
{ "docVote": 1, "loc": "2dsphere" }).explain()
But this is the result:
    {
    "cursor" : "S2Cursor",
    "isMultiKey" : true,
    "n" : 10,
    "nscannedObjects" : 44283,
    "nscanned" : 648117,
    "nscannedObjectsAllPlans" : 44283,
    "nscannedAllPlans" : 648117,
    "scanAndOrder" : true,
    "indexOnly" : false,
    "nYields" : 13,
    "nChunkSkips" : 0,
    "millis" : 12632,
    "indexBounds" : {
    },
    "nscanned" : 648117,
    "matchTested" : NumberLong(46642),
    "geoTested" : NumberLong(46642),
    "cellsInCover" : NumberLong(8),
    "server" : "*********"
    }
If i remove sort from the query i obtain this:
    {
        "cursor" : "S2Cursor",
        "isMultiKey" : true,
        "n" : 10,
        "nscannedObjects" : 10,
        "nscanned" : 25,
        "nscannedObjectsAllPlans" : 10,
        "nscannedAllPlans" : 25,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 3,
        "indexBounds" : {
        },
        "nscanned" : 25,
        "matchTested" : NumberLong(10),
        "geoTested" : NumberLong(10),
        "cellsInCover" : NumberLong(8),
        "server" : "******"
    }
So, why index is not used to sort results? From this documentation: http://docs.mongodb.org/manual/applications/geospatial-indexes/ http://docs.mongodb.org/manual/tutorial/sort-results-with-indexes/ I understand that MongoDb support "A compound index with scalar index fields (i.e. ascending or descending) as a prefix or suffix of the 2dsphere index field" and that "If the sort document is a subset of a compound index and starts from the beginning of the index, MongoDB can use the index to both retrieve and sort the query results."
What am I missing?
Thanks in advance
- duplicates
 - 
                    
SERVER-10801 Allow compound geo indexes to be able to provide sort
-         
 - Backlog
 
 -