MongoDb 2.6.1 Error: 17444 - “Legacy point is out

2019-07-21 11:58发布

问题:

After upgrading MongoDb to 2.6.1 in my System n i get sometimes the following error:

Legacy point is out of bounds for spherical query

ErrorCode 17444

Here: https://github.com/mongodb/mongo/blob/master/src/mongo/db/geo/geoquery.cpp#L73 I can see that this is raised by mongo db due to some invalid data.

// The user-provided point can be flat.  We need to make sure that it's in bounds.
if (isNearSphere) {
    uassert(17444,
            "Legacy point is out of bounds for spherical query",
            centroid.flatUpgradedToSphere || (SPHERE == centroid.crs));
}

But currently i can't figure why and how to prevent it.

My Code looks like this:

IEnumerable<BsonValue> cids = companyIds.ToBsonValueArray();


    return Collection.Find(
                            Query.And(
                               Query.In("CompanyId", cids),
                               Query.Near("Location", location.Geography.Longitude, location.Geography.Latitude, location.Radius / 6371000, true))).ToList();

Stacktrace:

QueryFailure flag was Legacy point is out of bounds for spherical query (response was { "$err" : "Legacy point is out of bounds for spherical query", "code" : 17444 }). at MongoDB.Driver.Internal.MongoReplyMessage1.ReadFrom(BsonBuffer buffer, IBsonSerializationOptions serializationOptions) at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializer serializer, IBsonSerializationOptions serializationOptions) at MongoDB.Driver.Operations.QueryOperation1.GetFirstBatch(IConnectionProvider connectionProvider)

回答1:

You're using MongoDB 2.6.1 or higher because the code you're looking at was added as a fix for a JIRA-13666 issue.

The problem was that some $near queries would crash MongoDB server when called with legacy coordinates that are out of range.

You're probably sending coordinates that are out of range. The part of the code that checks longitude and latitude when doing $near queries with max distance (GeoParser::parsePointWithMaxDistance method in geoparser.cpp):

bool isValidLngLat(double lng, double lat) {
    return lat >= -90 && lat <= 90 && lng >= -180 && lng <= 180;
}

If the coordinates are out of range centroid.flatUpgradedToSphere will be false and that will cause the error you're receiving.

You should either change your coordinates to be in range or set spherical parameter to false to avoid getting this error.

Query.Near("Location", location.Geography.Longitude, 
           location.Geography.Latitude, location.Radius / 6371000, false)