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