0

I need to calculate the total distance traveled between a collection of points. They all have a timestamp to get them in the right order. I'm only allowed to use MongoDB.

Sample collection:

{
    "_id" : ObjectId("596cd354241aa3174056fb98"),
    "spelerID" : 1,
    "timestamp" : ISODate("2017-02-01T19:00:00.000Z"),
    "coordinates" : {
        "type" : "Point",
        "coordinates" : [ 
            4.29870386367084, 
            50.8357637566422
        ]
    }
}

/* 2 */
{
    "_id" : ObjectId("596cd354241aa3174056fbb4"),
    "spelerID" : 1,
    "timestamp" : ISODate("2017-02-01T19:00:01.000Z"),
    "coordinates" : {
        "type" : "Point",
        "coordinates" : [ 
            4.29868458167181, 
            50.8357575419868
        ]
    }
}

/* 3 */
{
    "_id" : ObjectId("596cd354241aa3174056fbce"),
    "spelerID" : 1,
    "timestamp" : ISODate("2017-02-01T19:00:02.000Z"),
    "coordinates" : {
        "type" : "Point",
        "coordinates" : [ 
            4.29867536067721, 
            50.8357376028214
        ]
    }
}

I have no idea how to calculate the distance between those points in the right order.

Brent
  • 21
  • 1
  • 6

1 Answers1

0

MongoDB can performance the distance between two points by using $near or $geoNear.

Returns documents in order of proximity to a specified point, from the nearest to farthest. geoNear requires a geospatial index. https://docs.mongodb.com/manual/reference/command/geoNear/

In your case I don't think this will resolve the issue since you want to perform multiple distance calculation between multiple points.

MongoDB can be instruct to create very complex algorithm by using MapReduce. Even if I'm not expert in GIS application you could try to create a MapReduce routine with a Function that compute the distance between multiple points ordered as you like.

MongoDB - MapReduce

This link can help you to write a JS function to be used in Map Reduce: Calculate distance between two latitude-longitude points? (Haversine formula)

Good Luck!

Daniele Tassone
  • 2,104
  • 2
  • 17
  • 25