# GEOADD

> Add members with longitude and latitude to a geospatial index stored as a sorted set.

URL: https://docs.hotkv.com/commands/geoadd/

```
GEOADD key [NX | XX] [CH] longitude latitude member [longitude latitude member ...]
```

- Group: Geospatial
- Complexity: O(log N) per member
- HotKV Cloud: all plans
- On-Premise: no license needed

`GEOADD` stores each `member` with its position in the sorted set at `key`,
creating the key if needed. The position is encoded as the member's score, so
you can also use sorted set commands such as [ZRANGE](https://docs.hotkv.com/commands/zrange/index.md),
[ZREM](https://docs.hotkv.com/commands/zrem/index.md) and [ZCARD](https://docs.hotkv.com/commands/zcard/index.md) on the same key. Adding
a member that already exists updates its position.

Longitude comes first, then latitude. Valid longitudes are -180 to 180 and
valid latitudes -85.05112878 to 85.05112878 (the poles are not covered).

## Options

| Option | Effect |
| --- | --- |
| `NX` | Only add new members; never update existing ones. |
| `XX` | Only update existing members; never add new ones. |
| `CH` | Return the number of members added or moved, instead of only added. |

`NX` and `XX` cannot be combined. The options come before the first
longitude.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Default | Integer: members added | Integer |
| With `CH` | Integer: members added plus members whose position changed | Integer |

Error replies:

- `ERR syntax error` when `NX` and `XX` are combined or the triplets are incomplete.
- `ERR value is not a valid float` for a coordinate that is not a number.
- `ERR invalid longitude,latitude pair 200.000000,10.000000` (with your values) for an out-of-range position.
- `WRONGTYPE` if the key holds a value that is not a sorted set.

Nothing is stored when any argument is invalid.

## Examples

```
127.0.0.1:6379> GEOADD cities -0.1276 51.5072 london 2.3522 48.8566 paris 4.3517 50.8503 brussels
(integer) 3
127.0.0.1:6379> GEOADD cities CH 4.3517 50.8503 brussels 4.9041 52.3676 amsterdam
(integer) 1
127.0.0.1:6379> GEOADD cities NX 0 0 paris
(integer) 0
127.0.0.1:6379> GEOADD cities 200 10 nowhere
(error) ERR invalid longitude,latitude pair 200.000000,10.000000
```

## Compatibility

Same as Redis 7 and Valkey 8. Members get the same scores as in Redis, so
geospatial sorted sets can be copied between the two with
[ZRANGE](https://docs.hotkv.com/commands/zrange/index.md) and [ZADD](https://docs.hotkv.com/commands/zadd/index.md).

## See also

[GEOPOS](https://docs.hotkv.com/commands/geopos/index.md), [GEODIST](https://docs.hotkv.com/commands/geodist/index.md), [GEOSEARCH](https://docs.hotkv.com/commands/geosearch/index.md),
[GEOHASH](https://docs.hotkv.com/commands/geohash/index.md), [ZREM](https://docs.hotkv.com/commands/zrem/index.md)