MongoDB打印两点之间的距离



当我在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,
        "key" : "myLocation",
        "spherical": true,
        "distanceField": "distance",
        "distanceMultiplier": 0.000621371
    }}
]).pretty()

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

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

如果有多个2dsphere,则应指定一个"key"

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

让我们用一个简单的例子来理解它。考虑一个简单的收集商店

1.创建集合

db.createCollection('shops')

2.在商店集合中插入文档

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.在";地址";字段

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]是提取文档的中心位置或所需位置。

距离字段:";shopDistance",$geoNear聚合器返回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 }

在这里购物距离将以米为单位。

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

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

在文档中,它说如果您使用遗留对,例如:near:[long,lat],则以弧度指定最大距离。如果您使用GeoJSON,例如:靠近:{type:"Point",坐标:[long,lat]},然后以为单位指定最大距离。

使用$geoNear获取给定位置与用户之间的距离。

db.users.aggregate([
    {"$geoNear": {
        "near": {
            "type": "Point",
            "coordinates": [ longitude, latitude]
        },
        "distanceField": "distance",
        "distanceMultiplier": 1/1000,
        "query": {/* userConditions */},
    }}
]).pretty()

最新更新