MongoDB PHP uses to store latitude and longitude, distance query

https://blog.csdn.net/qq_40012295/article/details/84861466

Create a database using the command:

use user
using the command to create the collection:

db.createCollection (user)
using the command to create 2dsphere index:

db.user.createIndex ({LOCATION: "2dsphere"})
the PHP code into latitude and longitude data:

public function uploadMongoDBLocation()
{
$document = [
'name' => '张三',
'location' => [
(float)115.036545,
(float)36.313916,
],
];
$manager = new \MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');
$bulk = new \MongoDB\Driver\BulkWrite;
$bulk->insert($document); // 可连续使用多个insert
$writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);/
$res = $manager->executeBulkWrite('user.user', $bulk, $writeConcern);
return $res;
}
PHP代码查询距离:

        Note: This command used depending on the version, there may be a difference between the usage, MongoDB version used here is 3.6.5

public function findMongoDBLocation()
{
$document = [
'geoNear' => 'user',
'near' => [
'type' => 'Point',
'coordinates' => [(float)'115.042725', (float)'36.312956'],
],
'spherical' => true,
'minDistance' => 0,
'maxDistance' => 100000,
'num' => 1000
];
$command = new \MongoDB\Driver\Command($document);
$manager = new \MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');
$cursor = $manager->executeCommand('user', $command);
$items = [];
foreach ($cursor as $document) {
$total = count($document->results);
if ($total > 0) {
foreach ($document->results as $result) {
$item = json_decode(json_encode($result->obj), true);
$item['distance'] = intval($result->dis);
unset($item['_id']);
$items[] = $item;
}
}
}
return $items;
}

---------------------
Author: Tom rookie
Source: CSDN
Original: https: //blog.csdn.net/qq_40012295/article/details/84861466
copyright notice : This article is a blogger original article, reproduced, please attach Bowen link!

 

 

 

https://zhuanlan.zhihu.com/p/51839804

MongoDB realize people nearby

 

Tencent recently to "drift bottles" Zhang Xiaolong proud of this product off the shelf, the saying goes: every man innocent pregnant Bi against himself, drift bottles this innocent, but some people take advantage of the spread of pornographic content or advertising, so in November No. 30 late at night, Tencent closed drift bottles. So as another familiar a micro-channel function "near" and it will not be off the shelf? Today, we will not discuss, is how near people realize, of course, there are many ways to achieve our discussion today: such as Redis, MySQL, Postgresql, MongoDB and so on. Well, today I would use LBS function to achieve MongoDB people nearby!

A, MongoDB Profile

MongoDB is a database NoSql, is a high-performance C ++ development, open source, schema-document database. MongoDB is document-oriented document JSON format, binary JSON (the BSON) stored in MongoDB. The official website address is: https://www.mongodb.com/  . The main features are: strong performance, high availability, horizontal scaling capabilities. Supports fully indexed, queries, support for data replication and failover between server recovery. Support for C ++, Ruby, Java, Python , PHP, C, C #, Javascript, Perl and so on.

Two, MongoDB and relational database comparison

Relational database concepts described databasedatabase Database Concepts MongoDB tablecollection database table / row collections rowdocument data recording / data field columnfield document / domain index indexindex

Third, preparation

Download and install mongodb database, download and install Robomongo , I do not know if you can consult the small series!

Fourth, data preparation

Bulk insert data into the database, switch to use mage mage database, perform db.user.insertMany (), user is the document name, insertMany () bulk insertion command, which passed json array, { 'name': 'Young Male ',' address': 'Jiangxi Nanchang Qingshan Lake market and quality Administration', 'gender': 1, loc: [115.993121,28.676436]} on behalf of a user data where gender: 0 1 female representatives, on behalf of men , loc is an array of latitude and longitude, of course, can be loc: {lng: 115.993067, lat: 28.67606}, but the official recommendation array.

db.user.insertMany([
 {'name':'杨帅哥', 'address':'江西省南昌市青山湖区市场和质量监督管理局', 'gender':1, loc:[115.993121,28.676436]},
 {'name':'王美眉', 'address':'江西省南昌市青山湖区创新一路职位小厨', 'gender':0, loc:[116.000093,28.679402]},
 {'name':'张美眉', 'address':'江西省南昌市青山湖区紫阳大道1916号', 'gender':0, loc:[115.999967,28.679743]},
 {'name':'李美眉', 'address':'江西省南昌市青山湖区云中城', 'gender':0, loc:[115.995593,28.681632]},
 {'name':'彭美眉', 'address':'江西省南昌市青山湖区北京东路1666号', 'gender':0, loc:[115.975543,28.679509]},
 {'name':'赵美眉', 'address':'江西省南昌市青山湖区市场一路大润发', 'gender':0, loc:[115.968428,28.669368]},
 {'name':'廖美眉', 'address':'江西省南昌市南昌县奥林匹克中心', 'gender':0, loc:[116.035262,28.677037]},
 {'name':'余帅哥', 'address':'江西省南昌市南昌县科技学院瑶湖校区', 'gender':1, loc:[116.02477,28.68667]},
 {'name':'吴帅哥', 'address':'江西省南昌市青山湖区创新一路母婴店', 'gender':1, loc:[116.002384,28.683865]},
 {'name':'何帅哥', 'address':'江西省南昌市青山湖区紫阳大道2999号', 'gender':1, loc:[116.000821,28.68129]},
])

Fifth, the index set 2d

Because the data stored on a two-dimensional plane to my point, LBS want to query, to set 2d index. db.user.createIndex ({ 'loc': "2d"}) is where indexed fields loc.

Sixth, the query 200 meters close to people

Query nearby people, first of all guidance latitude and longitude of the current location of the user, but also to get away if you want to not only get the data, you can use $ geoNear instruction, if you can use to calculate the distance yourself or $ near $ geoWithin then calculate the distance in the manual. Here the use of $ geoNear instruction inquiry 2000m of people nearby.

db.user.aggregate({  $geoNear:{  near: [115.999567,28.681813], // 当前坐标  spherical: true, // 计算球面距离  distanceMultiplier: 6378137, // 地球半径,单位是米,那么的除的记录也是米  maxDistance: 2000/6378137, // 过滤条件2000米内,需要弧度  distanceField: "distance" // 距离字段别名  } }) 

Here so far, the use of mongodb index 2d plane to complete the search for nearby friends, if you want to know more, welcome to pull up small series!

 

Guess you like

Origin www.cnblogs.com/rxbook/p/10954762.html