MongoDB 打印两点之间的距离

MongoDB print distance between two points

当我在 MongoDB 上触发此查询时,我得到了距指定坐标 500 英里附近的所有地点。但是我想知道指定坐标和结果位置之间的确切距离。

db.new_stores.find({ "geometry": { $nearSphere: { $geometry: { type: "Point", coordinates: [ -81.093699, 32.074673 ] }, $maxDistance: 500 * 3963 } } } ).pretty()

我的输出如下:

{
    "_id" : ObjectId("565172058bc200b0db0f75b1"),
    "type" : "Feature",
    "geometry" : {
        "type" : "Point",
        "coordinates" : [
            -80.148826,
            25.941116
        ]
    },
    "properties" : {
        "Name" : "Anthony's Coal Fired Pizza",
        "Address" : "17901 Biscayne Blvd, Aventura, FL"
    }
}

我也想知道这个地方离指定坐标的距离。我在几何上创建了 2dsphere 索引。

您可以使用 $geoNear 聚合管道阶段来生成距查询点的距离:

 db.new_stores.aggregate([
    { "$geoNear": {
        "near": {
            "type": "Point",
            "coordinates": [ -81.093699, 32.074673 ]
        }, 
        "maxDistance": 500 * 1609,
        "spherical": true,
        "distanceField": "distance",
        "distanceMultiplier": 0.000621371
    }}
]).pretty()

这允许您指定 "distanceField",这将在输出文档中生成另一个字段,其中包含距查询点的距离。您还可以根据需要使用 "distanceMultiplier" 将任何转换应用于输出距离(即米到英里,并注意所有 GeoJSON 距离 returned 以米为单位)

还有具有类似选项的 geoNear 命令,但它当然不会 return 光标作为输出。

最大距离 -> 可选。文档可以距中心点的最大距离。 MongoDB 将结果限制在距中心点指定距离内的那些文档。

如果指定点是 GeoJSON,则以米为单位指定距离;如果指定点是传统坐标对,则以弧度为单位指定距离。

在文档中它说如果你使用 legacy pairs ,例如:near : [long , lat] ,然后在 radians[=21] 中指定 maxDistance =]. 如果您使用 GeoJSON,例如:near : { type : "Point" , coordinates : [long ,lat] }, 然后在 .

中指定最大距离

MongoDB 提供了一个 $geoNear 聚合器,用于计算 collection 中具有 GeoJson 坐标的文档的距离。

让我们通过一个简单的例子来理解它。 考虑一个简单的 collection 商店

1.创建Collection

db.createCollection('shops')

2。在商店中插入文档 collections

db.shops.insert({name:"Galaxy store",address:{type:"Point",coordinates:[28.442894,77.341299]}})

db.shops.insert({name:"A2Z store",address:{type:"Point",coordinates:[28.412894,77.311299]}})

db.shops.insert({name:"Mica store",address:{type:"Point",coordinates:[28.422894,77.342299]}})

db.shops.insert({name:"Full Stack developer",address:{type:"Point",coordinates:[28.433894,77.334299]}})

3。在“地址”字段上创建 GeoIndex

db.shops.createIndex({address: "2dsphere" } )

4.现在使用 $geoNear 聚合器 找出有距离的文件。

db.shops.aggregate([{$geoNear:{near:{type:"Point",coordinates:[28.411134,77.331801]},distanceField: "shopDistance",$maxDistance:150000,spherical: true}}]).pretty()

此处坐标:[28.411134,77.331801]是中心位置或需要的位置,从那里获取文件。

distanceField:"shopDistance", $geoNear Aggregator return shopDistance作为结果中的字段。

结果:

{ "_id" : ObjectId("5ef047a4715e6ae00d0893ca"), "name" : "Full Stack developer", "address" : { "type" : "Point", "coordinates" : [ 28.433894, 77.334299 ] }, "shopDistance" : 621.2848190449148 }
{ "_id" : ObjectId("5ef0479e715e6ae00d0893c9"), "name" : "Mica store", "address" : { "type" : "Point", "coordinates" : [ 28.422894, 77.342299 ] }, "shopDistance" : 1203.3456146763526 }
{ "_id" : ObjectId("5ef0478a715e6ae00d0893c7"), "name" : "Galaxy store", "address" : { "type" : "Point", "coordinates" : [ 28.442894, 77.341299 ] }, "shopDistance" : 1310.9612119555288 }
{ "_id" : ObjectId("5ef04792715e6ae00d0893c8"), "name" : "A2Z store", "address" : { "type" : "Point", "coordinates" : [ 28.412894, 77.311299 ] }, "shopDistance" : 2282.6640175038788 }

这里的 shopDistance 以米为单位。