Getting started with the geo-spatial index of MongoDB Learning notes

Source: Internet
Author: User

Geo-Spatial Index:

A geospatial index that can be used to process geo-location-based queries.

Point: Used to specify the exact location, we take restaurants as an example:

Db.restaurants.insert ({name: "Citi", loc: {type: "point", Coordinates: [52.37, 5.21]}})

Db.restaurants.insert ({name: "SAP", loc: {type: "point", Coordinates: [51.91, 4.41]}})

Db.restaurants.insert ({name: "IBM", loc: {type: "point", Coordinates: [52.36, 4.89]}})

Create 2dsphere Index: (longitude default range is 180 to 180, we change to-500 to 500)

Db.restaurants.ensureIndex ({loc: "2dsphere"},{min: -500, max:500})

Search for restaurants within 40000 meters of the maximum distance from the specified location:

Db.restaurants.find ({loc: {$geoNear: {$geometry: {type: ' point ', coordinates:[52.33,5.51]}, $maxDistance: 40000}})

Review the execution plan and discover that the 2dsphere index is used:

Db.restaurants.find ({loc: {$geoNear: {$geometry: {type: "point", coordinates:[52.33,5.51]}, $maxDistance: 40000}}). Explain (true)

By default, running a query with the Find () function is sufficient, but MongoDB also provides the Geonear function, which also provides the distance from the specified point to each record in the query results, as well as some additional diagnostic information.

Db.runcommand ({geonear: "Restaurants", near: {type: ' point ', coordinates: [52.33, 5.51]}, spherical:true})

There are many geospatial types and functions, but similar to the use of points, here is not one by one for example, if you use it in the work, you can check the official website.

Getting started with the geo-spatial index of MongoDB Learning notes

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.