0

I have a bunch of points {A, B, C, ...., X} and I want to store their distances in a matrix. One extra complication is that the distance from A to B and not the same as the distance from B to A, they are asymmetric.

My aim is to store this matrix in a collection in MongoDB but I really don't know how to, is that possible? any advice/ guidance is much appreciated.

MTA
  • 739
  • 2
  • 9
  • 29
  • you can provide a more complete example of the structure you want to save? These points are geographical coordinates? – Leonardo Delfino Feb 05 '15 at 23:59
  • @LeonardoDelfino The points are geographical coordinates; latitude and longitudes. I want to store the distances between these points, for any two points the distance from point A to point B is not the same as the distance from point B to point A. Essentially I want to store a distance matrix in mongo and I want it to be able to give it any two points (lets say B and D) and then retrieve the distance between them. – MTA Feb 06 '15 at 11:24

2 Answers2

0

MongoDB supports store Geospatial objects providing a particular way to create indexes and perform query these objects.

Take a look at :

http://docs.mongodb.org/manual/applications/geospatial-indexes/

and see if that is what you are looking for.

Leonardo Delfino
  • 1,488
  • 10
  • 20
  • I don't know how 2Dsphere index will work.. Basically I have a table that stores all the places (with longitude and latitudes) but I want to calculate the distances using google maps API and insert them in a different collection in Mongo. – MTA Feb 06 '15 at 12:06
0

What measure of "distance" are you using, out of curiosity? Technically, you can't call a function d(X, Y) a "distance" unless d(X, Y) = d(Y, X) always. Distance between points on a sphere is symmetric so you can't be using that metric. If all you want to do is store and retrieve values d(X, Y), just store documents like

{
    "from" : X,
    "to" : Y,
    "distance" : 691
}

where X and Y are whatever kind of values are appropriate. Put an index on { "from" : 1, "to" : 1 } and then define

function d(X, Y) {
    return db.distances.findOne({ "from" : X, "to" : Y }).distance
}
wdberkeley
  • 11,531
  • 1
  • 28
  • 23