gregavola
gregavola

Reputation: 2539

MongoDB $near Returning Max Results with Max Distance

I have the following mongo query (written in PHP)

 $q = array("created_at" => array("\$gte" => $mdate), "icv" => 1, "loc" => array("\$near" => array($latitude, $longitude), "\$maxDistance" => 5));

Which is basically:

db.collection.find({loc: {$near: [XX,YY]}, $maxDistance: 5}, "vid": 1, "created_at":{$gte: "SOMEDATE"}});

I would like to find ALL the documents that match this query, not just the 100 that it returns by default. If i set the limit on this query, it goes out side the distance distance.

Any suggestions?

Upvotes: 0

Views: 1118

Answers (1)

jmikola
jmikola

Reputation: 6922

In this mailing list post, Eliot mentions that $near does not use a cursor, so its results are limited to either 4MB or 100 documents, whichever is reached first. The current documentation says the same, but this is true only for 2d indexes (the documentation should be fixed by DOCS-1841).

If you are storing points in GeoJSON and using 2dsphere indexes (new in version 2.4), $near queries do utilize cursors and should not have a hard upper limit of 100 documents.

Consider the following examples, first using a 2d index:

> var point = [0,0];
> for(i=0;i<200;i++) db.foo.insert({x: point});
> db.foo.ensureIndex({x: "2d"});
> db.foo.find({x: {$near: point}}).count(true);
100
> db.foo.find({x: {$near: point}}).limit(200).count(true);
100

Then using a 2dsphere index with equivalent point data:

> var point = {type: "Point", coordinates: [0,0]};
> for(i=0;i<200;i++) db.bar.insert({x: point});
> db.bar.ensureIndex({x: "2dsphere"})
> db.bar.find({x: {$near: point}}).count(true)
200
> db.bar.find({x: {$near: point}}).limit(150).count(true)
150

Upvotes: 1

Related Questions