Geospatial index of MongoDB

Source: Internet
Author: User

Recently, I have been working on lbs-based projects, and I have not found a simple and convenient method for landmark coordinate indexes and coordinate-based queries, when optimizing Mongo indexes, we found that there were actually geo indexes.

1. Create a field

Recommended usage: {LOC: [longpolling, latitude]}

You can also: {LOC: {Lon: longpolling, Lat: latitude }}

Loc, I use poi

2. Create an index. Note: Only one geospatial index can be created for each collection.

DB. Places. ensureindex ({LOC: "2D" },{ min:-500, Max: 500 })

If dB. Places. ensureindex ({LOC: "2D"}), the default value range is-180 to 180, that is, the latitude and longitude range.

3. Query

Note: 1. Sorting by distance by default;

2. Convert the 111.12 distance (once approximately 111.12 km) using the km, or by 69 (for miles)

100. The default value is 100 closest points. Note: The default value is limit)

DB. collection. Find ({LOC: {$ near: [30.28009, 120.12857]})

For the latest updates, see http://www.shareach.com /? P = 136

Related Article

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.