mongodbmatrixdistancepointasymmetric

storing a matrix as a collection in MongoDB


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.


Solution

  • 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
    }