Will there be Mongo $ near return documents for which any point in MultiPoint is within range?

When using the $near / $geoNear / $geoWithin MultiPoint GeoJson object, will the document be returned if any points are within the range, or should they all be in the range?

+6
source share
1 answer

Case for "near"

The calculated distance will always be at the β€œclosest” point of any GeoJSON object. The same goes for Polygon or MultiPolygon, and indeed all GeoJSON objects that are valid for storage.

Consider this:

 { "location": { "type": "MultiPoint", "coordinates": [ [ -73.9580, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.9814, 40.7681 ] ] } } 

And if we use the $geoNear aggregation as a means to show us the distance from this place:

 db.geo.aggregate([ { "$geoNear": { "near": { "type": "Point", "coordinates": [ -73.97661209106445, 40.774561857347244 ] }, "spherical": true, "distanceField": "distance" }} ]) 

This tells us that the distance is considered equal to 824 m.

Now, if you viewed each β€œpoint” as its own document instead in the collection and performed the same request process:

 { "location" : { "type" : "Point", "coordinates" : [ -73.9814, 40.7681 ] }, "distance" : 824.837276194968 } { "location" : { "type" : "Point", "coordinates" : [ -73.9737, 40.7648 ] }, "distance" : 1114.0666715946495 } { "location" : { "type" : "Point", "coordinates" : [ -73.958, 40.8003 ] }, "distance" : 3266.4720692258156 } { "location" : { "type" : "Point", "coordinates" : [ -73.9498, 40.7968 ] }, "distance" : 3351.9091229713567 } 

Then you see that the different distances of each point from the start point is a query, where in the first case, for the whole object, only the "closest" was considered.

So, there is evidence that the distance viewed with $near / $geoNear or always only , is the closest starting point used in the query.

Case for $ geoWithin

The $geoWithin , however, is different. Consider the original MultiPoint document, and then this query:

 db.geo.find({ "location": { "$geoWithin": { "$geometry": { "type": "Polygon", "coordinates": [ [ [ -73.98382186889648, 40.75961056635002 ], [ -74.00030136108398, 40.782751138401245 ], [ -73.97317886352539, 40.78950978441435 ], [ -73.95910263061523, 40.7720918760227 ], [ -73.98382186889648, 40.75961056635002 ] ] ] } } } }) 

This will not return any result, and this will not happen because the "not all" components of the Point object lie within the Polygon tge used in the request. But if you considered each point as a separate document:

 { "_id" : ObjectId("564d5efd9f28c6e0feabcef8"), "location" : { "type" : "Point", "coordinates" : [ -73.9737, 40.7648 ] } } { "_id" : ObjectId("564d5efd9f28c6e0feabcef9"), "location" : { "type" : "Point", "coordinates" : [ -73.9814, 40.7681 ] } } 

Then two points will be visible inside the polygon. But since they are not stored as separate documents, but as part of MutiPoint, then if the form does not contain all parts of this object, then the result will be false and the document will not be returned.

The same is true for all GeoJSON objects, which essentially contain a set of "Point" in some representation.

+7
source

All Articles