GEOADD(3valkey)
π GEOADD β the valkey geospatial glow-up π
ok so this is GEOADD from the valkey command manual (section 3valkey), and bestie, it is the way to drop pins on a map. let's get into it πΊοΈβ¨
β¨ NAME β¨
geoadd β adds one or more members to a geospatial index. the key gets created if it doesn't exist. no cap.
π SYNOPSIS π
GEOADD key [NX | XX] [CH] longitude latitude member [longitude latitude
member ...]
πΈ DESCRIPTION πΈ
so basically you're adding geospatial items β that's (longitude, latitude, name) triples β to the key you picked. the data lives inside that key as a sorted set, and it's stored in a way that lets you query everything later with the valkey-geosearch(7) GEOSEARCH command. it's giving organized. it's giving queryable. π
the command takes args in the standard x,y format, which means longitude comes BEFORE latitude. do not flip them, i am begging. π
there are limits on which coordinates you can index: areas super close to the poles are not indexable. the exact limits, as specified by EPSG:900913 / EPSG:3785 / OSGEO:41001, are:
- valid longitudes are from
-180to180degrees. - valid latitudes are from
-85.05112878to85.05112878degrees.
if you try to index coordinates outside those ranges, the command will report an error. so like, don't.
π note: there is noGEODELcommand, because you can just usevalkey-zrem(7)ZREMto remove elements. the Geo index structure is literally just a sorted set. the way that it's just a sorted set?? iconic behavior.
π«Ά GEOADD options π«Ά
GEOADD also gives you these options:
XXβ only update elements that already exist. never add new elements. it's giving "if you know you know, but if you're new, no entry."NXβ don't update already-existing elements. always add new elements. brand new besties only β¨CHβ modifies the return value from the number of new elements added to the total number of elements changed (CHis short for changed). "changed elements" means new elements added plus existing elements whose coordinates got updated. so elements you list that end up with the same score they already had are not counted. note: normally, the return value ofGEOADDonly counts the number of new elements added.
β οΈ note:XXandNXare mutually exclusive. pick one, you cannot have both, it's not that kind of party.
π₯ How does it work? π₯
the sorted set gets populated using a technique called Geohash. latitude and longitude bits get interleaved to form a unique 52-bit integer, and we know a sorted set double score can represent a 52-bit integer without losing precision. math girlies stay winning π
this format allows bounding box and radius querying by checking the 1+8 areas needed to cover the whole shape, then discarding elements outside it. the areas get checked by calculating the range of the box covered, removing enough bits from the less significant part of the sorted set score, and computing the score range to query in the sorted set for each area. yes it's a whole thing. yes it slaps.
π What Earth model does it use? π
the model assumes Earth is a sphere, since it uses the Haversine formula to calculate distance. that formula is only an approximation when applied to the actual Earth, which is not a perfect sphere. the introduced errors are not an issue when it's used by, say, social networks and similar apps that need this kind of querying. however, in the worst case the error can be up to 0.5%, so for error-critical applications you might wanna consider other systems. not me being honest about my flaws π
β REPLY β
valkey-protocol(7) Integer reply: when used without optional arguments, it's the number of elements added to the sorted set (excluding score updates). if the CH option is specified, it's the number of elements that were changed (added or updated).
π COMPLEXITY π
O(log(N)) for each item added, where N is the number of elements in the sorted set.
π¦ ACL CATEGORIES π¦
@geo @slow @write
π HISTORY π
- available since:
3.2.0 - changed in
6.2.0: added theCH,NXandXXoptions.
π EXAMPLES π
127.0.0.1:6379> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
127.0.0.1:6379> GEODIST Sicily Palermo Catania
"166274.1516"
127.0.0.1:6379> GEORADIUS Sicily 15 37 100 km
1) "Catania"
127.0.0.1:6379> GEORADIUS Sicily 15 37 200 km
1) "Palermo"
2) "Catania"
π SEE ALSO π
geodist(3valkey), geohash(3valkey), geopos(3valkey), georadius(3valkey), georadiusbymember(3valkey), georadiusbymember_ro(3valkey), georadius_ro(3valkey), geosearch(3valkey), geosearchstore(3valkey)
ok that's the whole page, you're so welcome π«Ά this was valkey 9.0.0, dated 2025-10-21, straight from the Valkey Command Manual. go add some coordinates, bestie, and remember: longitude first. always. π β¨
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms