Question

I have been running a geospatial query using a compound index.

After upgrading to MongoDB 2.6, the order of the result set has changed such that the results are no longer being sorted by distance for the following query:

db.properties.find({ 
    "address.uppercase": { "$regex": "^14529" }, 
    "address.location": { 
        "$near": { 
            "$geometry": { 
                "type": "Point", 
                "coordinates": [ -122.2103, 47.6154 ] 
            }
        }
    }
})

The index on the properties collection is configured as:

{ "address.uppercase": 1, "address.location": "2dsphere" }

In 2.4, the query returned all addresses where "address.uppercase" starts with "14529", ordered by the address distance from location [ -122.2103, 47.6154 ].

After upgrading to 2.6, the same query now returns all addresses ordered by "address.uppercase".

Is there a way to specify the sort order of the results to be based on the geospatial portion of the query?

Was it helpful?

Solution 2

The bug has been confirmed to occur with a compound multi-key 2dsphere index. I.e. It will only exhibit itself if one of the documents in the collection have more than one value (i.e array) for one of the fields defined by the index.

10gen will be releasing the fix in MongoDB 2.6.2. See: https://jira.mongodb.org/browse/SERVER-13687

OTHER TIPS

Sorry but I do not see that behavior. With the example documents:

{ 
    "address" : { 
        "uppercase" : "14529", 
        "location" : { 
            "type" : "Point", 
            "coordinates" : [ -100.2014, 47.6154 ] 
        } 
    }
},
{
    "address" : {
        "uppercase" : "90210",
        "location" : {
            "type" : "Point",
            "coordinates" : [ -120.2014, 47.6154 ]
        }
    }
},
{
    "address" : { 
        "uppercase" : "14529", 
        "location" : { 
            "type" : "Point", 
            "coordinates" : [ -120.2014, 47.6154 ] 
        }
    }
}

And with the query:

db.address.find({
    "address.uppercase": { "$regex": "^14529" },
    "address.location": {
        "$near": {
            "$geometry": {
                 "type": "Point",
                 "coordinates": [ -122.2103, 47.6154 ]
            }
        }
    }
})

I get these in order of nearest :

{ 
    "address" : { 
        "uppercase" : "14529", 
        "location" : { 
            "type" : "Point", 
            "coordinates" : [ -120.2014, 47.6154 ]
        }
    }
},
{ 
    "address" : { 
        "uppercase" : "14529", 
        "location" : { 
            "type" : "Point", 
            "coordinates" : [ -100.2014, 47.6154 ]
        }
    }
}

And even with regressing the index type. Perhaps try dropping the index and re-creating it and see of you can reproduce. Otherwise if you possibly have an edge case example that does not fit these conditions, then your question could do with an edit to include the required detail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top