Nodejs+mongo realize the people near the search

Source: Internet
Author: User

Reference URL: http://blog.csdn.net/huangrunqing/article/details/9112227

Using MONGO as a storage, to achieve the people near the search has the innate advantage,

MongoDB Native supports geo-location indexing and can be used directly for location distance calculations and queries.

In addition, it is one of the most popular NoSQL databases today, with features such as collection-oriented storage, schema freedom, high performance, support for complex queries, support for full indexing, and so on, in addition to being able to support geo-location computing well.

First look at the data storage format I have in MONGO:

/* 0 */{  "_id": "1",  "username": "hyw",  "address": "Garden Village", "Location  ": [113.676557, 34.744776],  "b Loodtype ":" O ","  nickname ":" Huang Fu ",  " XZ ":" Capricorn ",  " tel ":" 123456 ",  " Birthday ":" 1989-12-13 ",  " sex " : "0",  "email": "[email protected]"}/* 1 */{  "_id": "999",  "username": "Uiouio",  "Address": "PPPPP P ",  " location ": [113.594452, 34.742136],  " Bloodtype ":" X ",  " nickname ":" Oooo ",  " XZ ":" Archer ",  "Tel": "909090",  "birthday": null,  "sex": "Male",  "email": "uuuu121"}

In fact, the most important command to use MONGO to search for nearby people is the Geonear command, which is explained as follows:

Geonear returns the DIS in the result set, if spherical is specified as true, the value of dis is radians and is not specified. If sphericial is not specified, the dis in the result is multiplied by 111 to be converted to km:
Specifying spherical is true, the dis in the result needs to be multiplied by 6371 to be converted to km:
--Get 500 meters (0.5 kilometers) nearby.
Db.runcommand ({geonear: ' UserInfo ', near:[113.676557,34.744778],spherical:true,maxdistance:0.5/6371, DISTANCEMULTIPLIER:6371,QUERY:{XZ: ' pisces '},num:10});

Where UserInfo is a collection of geo-location information (the so-called table in a relational database), maxdistance specifies the maximum radius of the search, and query specifies other search criteria, num (or limit) specifies the number of bars to return the result. Other specific parameters can refer to the official documentation http://docs.mongodb.org/manual/reference/command/geoNear/#dbcmd. geonear

The Nodejs code is very simple:

/** * Get nearby people */getnearuser:function (queryparams,callback) {var command = {};        Command.geonear = ' UserInfo '; Command.spherical = true;//If spherical is specified as true, the value of dis is radians, not specified is degree command.distancemultiplier = 6371000;//Specifies spherical        To true, the dis in the result needs to be multiplied by 6371 to convert to km: When the query is specified distancemultiplier, it will multiply this parameter by the distance to return var location = [];        Location.push (QUERYPARAMS.LNG);        Location.push (Queryparams.lat);        Command.near = location;        if (queryparams.distance) {command.maxdistance = queryparams.distance/6371000;        } if (queryparams.rows) {command.num = queryparams.rows;            } if (QUERYPARAMS.XZ) {var queryentity = {};            QUERYENTITY.XZ = QUERYPARAMS.XZ;        Command.query = queryentity;            } db.mongoConn.command (Command,function (Err,result) {if (err) {return callback (ERR);            }else{callback (Null,result.results);     }   }); }

  

Nodejs+mongo realize the people near the search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.