LEN

Mongo geoNear 使用笔记
php 环境 php7 + mongodb 扩展先上代码, 保存<?php use MongoDB\Driv...
扫描右侧二维码阅读全文
21
2017/06

Mongo geoNear 使用笔记

php 环境 php7 + mongodb 扩展

先上代码, 保存

<?php
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Manager;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\Exception ;

$MONGO_DBNAME = 'mogno_db'; // db name
$MONGO_COORDINATES_TABLE = 'mongo_coordinates_collection'; // collection name
$lng = 115.481529; // 经度
$lat = 40.048231; // 纬度
$user_id = 1000;
$data = array(
    'location' => array(
        'type' => "Point",
        'coordinates' => [(float)$lng, (float)$lat] // 坐标经度, 纬度
    ),
    'user_id' => (string)$user_id
);
try {
    $bulk = new BulkWrite(['ordered' => true])); //写操作排序 默认是有序true,如果写入失败,则剩余的操作将被中止;无序false 以任意顺序发送给服务器,以并行方式执行。发生任何操作后,任何操作失败都会报错
    $bulk->insert($data);
    //$bulk->update(['user_id' => (string)$user_id)], ['$set' => ['user_id' => '1']]);
    //$bulk->delete($data);
    //count($bulk);
    $manager = new Manager('mongodb://127.0.0.1:27017');            // 创建连接
    $writeConcern = new WriteConcern(WriteConcern::MAJORITY, 1000); // 写操作配置描述1参数固定 2参数timeout
    $manager->executeBulkWrite($MONGO_DBNAME . '.' . $MONGO_COORDINATES_TABLE, $bulk, $writeConcern);
} catch (Exception $e) {
    printf("Other error: %s\n", $e->getMessage());
}

mongo 命令:

db.mongo_coordinates_collection.insert({location:{type:"Point",coordinates:[115.481529,40.048231]},user_id:"1000"})

查询坐标代码

<?php
use MongoDB\Driver\Manager;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\Exception ;

$MONGO_DBNAME = 'mogno_db'; // db name
$MONGO_COORDINATES_TABLE = 'mongo_coordinates_collection'; // collection name
$lng = 115.481529; // 经度
$lat = 40.048231; // 纬度
// 查询坐标点范围1000米内的用户
$data = array(
    'geoNear' => $MONGO_COORDINATES_TABLE,
    'near' => array(
        'type' => 'Point',
        'coordinates' => array(
            (float)$lng, (float)$lat
        ),
    ),
    'limit' => 20,  // 取出最近20个文档
    'spherical' => true, // 球面几何计算距离
    'minDistance' => 0,
    'maxDistance' => 1000
);
try {
    $cmd = new Command($data);
    $manager = new Manager('mongodb://127.0.0.1:27017');            // 创建连接
    $cursor = $manager->executeCommand($MONGO_DBNAME, $cmd);
    $row_list = $cursor->toArray();
} catch (Exception $e) {
      printf("Other error: %s\n", $e->getMessage());
}

下行
{
    "results" : [ 
        {
            "dis" : 45293.388824048,
            "obj" : {
                "_id" : ObjectId("59476d22421aa944cf4907e3"),
                "location" : {
                    "type" : "Point",
                    "coordinates" : [ 
                        115.481529, 
                        40.048231
                    ]
                },
                "user_id" : "61826",
            }
        }, 
        {
            "dis" : 71000.6844228573,
            "obj" : {
                "_id" : ObjectId("59476c78421aa9384b7f78a1"),
                "location" : {
                    "type" : "Point",
                    "coordinates" : [ 
                        116.314887, 
                        41.051576
                    ]
                },
                "user_id" : "54761",
            }
        }, 
        {
            "dis" : 71000.6844228573,
            "obj" : {
                "_id" : ObjectId("5947776ad833dd4cef5d014c"),
                "location" : {
                    "type" : "Point",
                    "coordinates" : [ 
                        116.314887, 
                        41.051576
                    ]
                },
                "user_id" : "54761",
            }
        }, 
        {
            "dis" : 232274.43881401,
            "obj" : {
                "_id" : ObjectId("59476cb0421aa9638d54ec53"),
                "location" : {
                    "type" : "Point",
                    "coordinates" : [ 
                        116.806135, 
                        43.302393
                    ]
                },
                "user_id" : "55307",
            }
        }, 
        {
            "dis" : 302913.980156013,
            "obj" : {
                "_id" : ObjectId("59476c81421aa9679367ac73"),
                "location" : {
                    "type" : "Point",
                    "coordinates" : [ 
                        116.806135, 
                        44.302393
                    ]
                },
                "user_id" : "61830",
            }
        }, 
        {
            "dis" : 726113.565978571,
            "obj" : {
                "_id" : ObjectId("59476cc0421aa9039f6deab1"),
                "location" : {
                    "type" : "Point",
                    "coordinates" : [ 
                        107.165951, 
                        47.896315
                    ]
                },
                "user_id" : "58179",
            }
        }, 
        {
            "dis" : 1162765.83290674,
            "obj" : {
                "_id" : ObjectId("59476cd4421aa9188e3f9d33"),
                "location" : {
                    "type" : "Point",
                    "coordinates" : [ 
                        121.368664, 
                        24.23551
                    ]
                },
                "user_id" : "61495",
            }
        }, 
        {
            "dis" : 2996460.22837785,
            "obj" : {
                "_id" : ObjectId("59476c7d421aa9693876b5c4"),
                "location" : {
                    "type" : "Point",
                    "coordinates" : [ 
                        101.720351, 
                        0.0
                    ]
                },
                "user_id" : "51532",
            }
        }, 
        {
            "dis" : 7798452.71217744,
            "obj" : {
                "_id" : ObjectId("59476c84421aa93e280a6df3"),
                "location" : {
                    "type" : "Point",
                    "coordinates" : [ 
                        0.0, 
                        0.0
                    ]
                },
                "user_id" : "61820",
            }
        }, 
        {
            "dis" : 7798452.71217744,
            "obj" : {
                "_id" : ObjectId("59476c89421aa9638d54ec52"),
                "location" : {
                    "type" : "Point",
                    "coordinates" : [ 
                        0.0, 
                        0.0
                    ]
                },
                "user_id" : "51203",
            }
        }
    ],
    "stats" : {
        "nscanned" : NumberLong(58),
        "objectsLoaded" : NumberLong(58),
        "avgDistance" : 2120472.82282578,
        "maxDistance" : 7798452.71217744,
        "time" : 10
    },
    "ok" : 1.0
}

mongo命令 :

db.runCommand({geoNear:"t_online_host",near:{type:"Point",coordinates:[116.316989,40.049232]},spherical:true,minDistance:0,maxDistance:20000});

Mongo 官方文档地址: https://docs.mongodb.com/manual/reference/command/geoNear/

结合个人理解翻一下
alt

alt

Last modification:June 21st, 2017 at 02:24 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment