I. MongoDB INTRODUCTION (http://docs.mongoing.com/manual/applications/geospatial-indexes.html)
MongoDB provides a series of indexing and querying mechanisms to handle geospatial information. This section describes the geo-index characteristics of MongoDB. You can read the Geography index tutorial to learn a complete example of a geo query in MongoDB.
Surface
Before you store geographic data and write query criteria, you must first select a surface type, which will be used in calculations. The type you choose will affect how your data is stored, the type of index established, and the syntactic form of your query.
MongoDB offers two types of surfaces:
Spherical
If you need to calculate geographic data just like on a spherical surface similar to the earth, you can select a spherical surface to store the data so that you can use the 2dsphere index.
You can store location data as Geojson objects in the same way as axes: longitude and latitude . The coordinate reference system of the Geojson uses the WGS84 data.
Plane
If you need to calculate distances, like on a Euclidean plane, you can store location data in the form of normal coordinate pairs and use a 2d index.
Location data
If you select a spherical surface to calculate, you can choose to store the location data in one of the following two formats:
Geojson Object
Queries on GeoJSON are always based on spherical surfaces. The default coordinate reference system for Geojson uses WGS84 data.
2.4 New features: In version 2.4, the newly introduced storage and query support for Geojson. Prior to version 2.4, all geographic data was in the form of coordinate pairs.
Changes in version 2.6: Support for more Geojson types: Multipoint, multi-segment, Multipolygon, Geometry collection.
MongoDB supports the following Geojson objects:
Single point
Segment
Polygon
Multi-point
Multi-segment
- Multipolygon
Geometry collection
Common coordinate pairs
MongoDB supports spherical calculation of legacy coordinate pairs (normal coordinate pair) data using a 2dsphere index by converting the data to a Geojson point type.
If you choose a planar calculation and use a 2d index, you can store the data as only: Term: ' legacy coordinate pairs ' format.
Query operations
The MongoDB Geospatial query operation allows you to query:
Contains
MongoDB can query for locations that are fully contained within a specified polygon area. The containing query uses the $geoWithin operator.
Both indexes 2d and 2dsphere indexes support include queries. After version 2.2.3, MongoDB no longer requires an index for a containing query. However, these indexes can improve query performance.
Cross
MongoDB can query the intersection of a location and a specified geometry. These queries can only be used to query data stored on a spherical plane. These queries use $geoIntersects operators.
Only 2dsphere indexes support interleaving.
Neighboring
MongoDB can query and other points closest to a point. The proximity query uses the $near operator. The $near operator requires a 2d or 2dsphere index.
Geo-Spatial Index
MongoDB provides the following geographic index types to support geo-queries.
2dsphere
The 2dsphere index can support the following features:
Calculation on the spherical plane
Geojson object and backward compatibility with normal coordinate pairs.
Composite index. This composite index can contain a 2dsphere indexed field and some normal indexed fields, either ascending or descending, as prefixes or suffixes of the 2dsphere index.
2.4 New features: The ' 2dsphere ' index before version 2.4 is not available.
See
Querying the 2dsphere Index
2d
The 2d index supports the following features:
Use plane geometry to calculate
Common coordinate pairs (for example, points in a planar coordinate system)
As a composite index, an additional index is a key. By putting an additional key as the suffix of the 2d index key
See
Querying the 2d Index
Geo-spatial indexes and shards
You cannot use a geographic index as the Shard key index.
You can create and maintain a single geospatial index on a partitioned collection, using a partition key.
For partitioned geometries, queries that use $near operations are not supported. As an alternative, you can use the geonear command or $geoNear during the aggregation phase.
You can also use $geoWithin to query geospatial data.
Other resources
The following pages provide a complete documentation of geospatial indexes and queries
-
2dsphere Index
-
MongoDB's 2dsphere Index supports a query for geometric calculations on a spherical Earth-like sphere. The index supports storing data in the same way as geojson objects or normal coordinate pairs.
-
2d Index
-
MongoDB's 2d index supports storing data in normal coordinate pairs for MongoDB2.2 and previous
-
Geohaystack Index
-
The haystack index is a specially optimized index that returns small area results. For those queries that use spherical geometry, 2dsphere is a better choice.
-
2d indexing principle
-
Provides an explanation of the deeper nature of the geographic index. This material is not required for ordinary operators, but may be helpful for users who are removing obstacles or want to have a deeper understanding.
See
Geospatial Query Compatibility
Two. Example
Set up a places collection to store the location where the LOC field is used to store the regional data Geojson point.
Db.places.insert ({loc: {type:" Point", Coordinates: [-73.97,40.77]}, Name:"Central Park", Category:"Parks"}) Db.places.insert ({loc: {type:" Point", Coordinates: [-73.88,40.78]}, Name:"La Guardia Airport", Category:"Airport" })
Build an index
" 2dsphere " } )
The parameter is not 1 or-1, which is 2dsphere. You can also create composite indexes.
" 2dsphere " , Category:-11 })
3. Enquiry
$geometry represents a geometric picture of the query.
3.1 Querying the value of a polygon range
Type representation: Polygon Polygon
Db.places.Find({loc: {$geoWithin: {$geometry: {type:"Polygon", coordinates: [ [[0,0 ] , [ 3,6 ] , [ 6,1 ] , [ 0,0 ] ] ] } } } } )
3.2 Values near the query
Use $near to find nearby locations.
Db.places. Find ({loc: {$near: {$geometry: 'point' , < Longitude>, <latitude> ]}, inmeters> }})
3.3 Querying values within a circle
When querying a circle, you need to specify the center, radius.
Db.places. Find ({loc: {$geoWithin: {$centerSphere: - ]}} )
[-88, 30] for latitude and longitude, 10 for radius.
Address: http://blog.csdn.net/yonggang7/article/details/28109463
Two. Springmvc in the project is to use MONGODB query range point
2.1 Building an Index
//Deliver Datadeliver:{"Deliverarea": [ { "Area": [ { "District": "Congtai District", "Location": { "Lat": 36.613143702791, "LNG": 114.50521301419 }, "Street": "Chaoyang Road", "Street_number": "No. 5th" }, { "District": "Congtai District", "Location": { "Lat": 36.612694686043, "LNG": 114.50516800908 }, "Street": "Chaoyang Road", "Street_number": "No. 5th" }, { "District": "Congtai District", "Location": { "Lat": 36.612744657392, "LNG": 114.50573403138 }, "Street": "Chaoyang Road", "Street_number": "No. 5th" }, { "District": "Congtai District", "Location": { "Lat": 36.61310669522, "LNG": 114.50577903649 }, "Street": "Chaoyang Road", "Street_number": "No. 14th" } ] } ], "Deliverid": "D011122"}
Db.deliver.ensureIndex ({"deliverArea.area.location":"2dsphere "})
2.2 Query commands
//mongodb Shell
Db.deliver.aggregate ([{$geoNear: {near: {type: point , Coordinates: [114.495539 , 36.597626 " Span style= "color: #800000;" >deliverarea.area.location 10 num: 5 true } }])
// SPINGMVC Use
New Point (114.495539, 36.597626);
= Nearquery.near (location). MaxDistance (new Distance (Distance, metrics.miles)). Num (5). Spherical (True
= newaggregation (geonear (query,"deliver")
Aggregationresults<Deliver> results = mongotemplate.aggregate (aggregation, "deliver", deliver. Class);
// Spring uses mongooperations New Point ( -73.99171, 40.738868= nearquery.near (location). MaxDistance (new Distance, Metrics.miles)); Georesults<Restaurant> = operations.geonear (query, Restaurant. Class);
Using the deliver list data returned by Mongooperations Georesults, the deliver is wrapped in content, and the deliver data needs to be taken out of the content.
Reference: http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/
MongoDB Geospatial Indexes and queries