Mongo $geoNear query - incorrect nscanned number and incorrect results

跟風遠走 提交于 2019-12-06 05:25:13

问题


I have a collection with around 6k documents with 2dsphere index on location field, example below:

"location" : {
    "type" : "Point",
    "coordinates" : [ 
        138.576187, 
        -35.010441
    ]
}

When using the below query I only get around 450 docs returned with nscanned around 3k. Every document has a location, many locations are duplicated. Distances returned from GeoJSON are in meters, and a distance multiplier of 0.000625 will convert distances to miles. To test, I'm expecting max distance of 32180000000000 to return all the documents on the planet, ie 6000

db.x.aggregate([
{"$geoNear":{
    "near":{
        "type":"Point",
        "coordinates":[-0.3658702,51.45686]
    },
    "distanceField":"distance",
    "limit":100000,
    "distanceMultiplier":0.000625,
    "maxDistance":32180000000000,
    "spherical":true,
}}

])

Why dont I get 6000 documents returned? I'm unable to find the logic behind this behaviour from Mongo. I've found on the mongo forums: "geoNear's major limitation is that as a command it can return a result set up to the maximum document size as all of the matched documents are returned in a single result document."


回答1:


I'm pretty sure that mongodb has a limit of 16 MB on the results of $GeoNear. In https://github.com/mongodb/mongo/blob/master/src/mongo/db/commands/geo_near_cmd.cpp you can see that while the results of the geonear are being built, there's this condition

// Don't make a too-big result object.
if (resultBuilder.len() + resObj.objsize()> BSONObjMaxUserSize) {
    warning() << "Too many geoNear results for query " << rewritten.toString()
              << ", truncating output.";
    break;
}

And in https://github.com/mongodb/mongo/blob/master/src/mongo/bson/util/builder.h youll see its limited to 16 MB.

const int BSONObjMaxUserSize = 16 * 1024 * 1024;


来源:https://stackoverflow.com/questions/30155393/mongo-geonear-query-incorrect-nscanned-number-and-incorrect-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!