Search code examples
mongodbgeolocationgeospatial

Proximity query doesn't match LineString points between the ends


I have defined a "2dsphere" index for field loc in collection c:

db.c.createIndex( { loc : "2dsphere" } )

I have the following document in c collection. It is a LineString from [0, 0] to [10, 10]:

db.c.insert({"name": "myLine", "loc": { type: "LineString", coordinates: [ [ 0, 0 ], [ 10, 10 ] ] }})

Note that [5, 5] would be a point on the line. However, a proximity query (using the $near operator) centered at [5, 5] doesn't match the document:

db.c.find({
  loc: {
    $near: {
      $geometry: {
         type: "Point" ,
         coordinates: [ 5, 5 ]
       },
       $maxDistance: 0.1,
    } 
  } 
})

Maybe LineString doesn't work that way and only the end points ([0, 0] and [10, 10] in this case) are relevant from geo spatial queries point of view? In that case, is a limitation in MongoDB geo spacial queries implementation or is a limitation in the GJSON standard?

(Until I did this little experiment I thought that the difference between LineString and MultiPoint was that the former takes into account the line connecting the points while the later only takes into account the points at the ends of lines. After this experiment, I don't see the differences...).


Solution

  • I have to chosen between mongodb and postgis. But mongodb has this negative point. See the case: Find points near LineString in mongodb sorted by distance