Use of redis geographic location and MongoDB geographic index

Compare

  1. Longitude and latitude must be in the valid range. Longitudes range from -180 to 180, and latitudes range roughly from -90 to 90.
  2. 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.
  3. 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

  1. Add geographic location coordinates: GEOADD key name longitude latitude location name
  2. Delete geographic location (same as operating zset): ZREM key name location name
  3. Find nearby locations (returns [[location name, distance], [location name, distance], [location name, distance]...]):
    GEORADIUS key name longitude latitude maximum distance unit (m or km) WITHDIST (meaning with distance) count quantity number ASC (meaning from near to far)
  4. demo (with custom tools and methods, needs to be modified)

 MongoDB uses

  1. 2dsphere index needs to be established (2d index is not considered): db.table name.createIndex({"indexed field": "2dsphere"})
  2. The general data type is in JSON format, you can refer to the following structure: db.pos.createIndex({"location": "2dsphere"})
  3.  Query the nearby location and display the distance, the unit is meter
    db.getCollection('表名').aggregate([
    {$geoNear:{
        "near":{"type": "Point", "coordinates": [经度, 纬度]},
        "spherical":     true,
        "distanceField": "distance"
        }},
    {$skip:0},
    {$limit:20}
    ]);

Guess you like

Origin blog.csdn.net/qq_37575994/article/details/128312504