【问题标题】:How do I calculate the total distance traveled with MongoDB如何计算使用 MongoDB 行驶的总距离
【发布时间】:2017-07-17 14:55:25
【问题描述】:

我需要计算一组点之间的总距离。它们都有一个时间戳,可以让它们按正确的顺序排列。我只能使用 MongoDB。

样本收集:

{
    "_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
        ]
    }
}

我不知道如何以正确的顺序计算这些点之间的距离。

【问题讨论】:

    标签: mongodb 2dsphere


    【解决方案1】:

    MongoDB 可以通过使用 $near 或 $geoNear 来计算两点之间的距离。

    按照与指定点的接近程度从最近到最远的顺序返回文档。 geoNear 需要地理空间索引。 https://docs.mongodb.com/manual/reference/command/geoNear/

    在您的情况下,我认为这不会解决问题,因为您想在多个点之间执行多个距离计算。

    可以通过MapReduce来指导MongoDB创建非常复杂的算法。即使我不是 GIS 应用程序方面的专家,您也可以尝试创建一个带有函数的 MapReduce 例程,该函数可以计算您喜欢的多个点之间的距离。

    MongoDB - MapReduce

    此链接可以帮助您编写一个用于 Map Reduce 的 JS 函数: Calculate distance between two latitude-longitude points? (Haversine formula)

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      相关资源
      最近更新 更多