Longitude and latitude must be in the valid range. Longitudes range from -180 to 180, and latitudes range roughly from -90 to 90.
Redis uses the Zset structure to store, convert the longitude value and latitude value into one value, and turn the two-dimensional quantity into one-dimensional quantity to find the nearby position. The efficiency is extremely high, but it is limited to the plane and cannot be paged.
MongoDB is more flexible. By establishing a geographical location index, it can realize the nearby geographical location search. It can support plane, spherical, and pagination, and the function is more abundant and perfect.
Redis uses
Add geographic location coordinates: GEOADD key name longitude latitude location name
Delete geographic location (same as operating zset): ZREM key name location name
GEORADIUS key name longitude latitude maximum distance unit (m or km) WITHDIST (meaning with distance) count quantity number ASC (meaning from near to far)
demo (with custom tools and methods, needs to be modified)
MongoDB uses
2dsphere index needs to be established (2d index is not considered): db.table name.createIndex({"indexed field": "2dsphere"})
The general data type is in JSON format, you can refer to the following structure: db.pos.createIndex({"location": "2dsphere"})
Query the nearby location and display the distance, the unit is meter