Command: MAP.CSET
Overview
The MAP.CSET
command is used to create or overwrite a map at a specified key with a single key-value entry. If the key already exists, the previous map is completely replaced with the new one containing just the given mapKey
and value
.
This command is useful in situations where you want to ensure that a key holds a map with only one specific entry, replacing any old map that was present under that key.
Command Name
MAP.CSET
Description
Sets or updates a key-value pair inside a map stored at the specified key.
⚠️ Important: This command overwrites any existing map at the specified key. The new map will contain only the provided mapKey
and value
.
Use Cases
-
Replacing an existing device record with a new one.
-
Initializing a key with a map containing a single entry.
-
Resetting a user’s map data with a new object.
Syntax
MAP.CSET <key> <mapKey> <value>
-
<key>
: The top-level key to store the map under. -
<mapKey>
: The key inside the map. -
<value>
: A valid JSON string representing the value formapKey
.
Permissions
-
The user must have write permissions on the given key.
-
No special roles required beyond standard modification rights.
Examples
Example 1: Add a new map with one entry
localhost:9219> MAP.CSET user-001:devices device-6d6f6sa66d '{
"deviceName": "Pixel 7 Pro",
"osVersion": "Android 14",
"batteryLevel": "85%"
}'
Ok
localhost:9219> MAP.GET user-001:devices
Ok {
"deviceName": "Pixel 7 Pro",
"osVersion": "Android 14",
"batteryLevel": "85%"
}
Example 2: Overwrite map with a new entry
localhost:9219> MAP.CSET user-001:devices device-663abc5352 '{
"deviceName": "iPhone 14 Pro",
"osVersion": "iOS 16",
"batteryLevel": "75%"
}'
localhost:9219> MAP.GET user-001:devices
Ok {
"deviceName": "iPhone 14 Pro",
"osVersion": "iOS 16",
"batteryLevel": "75%"
}
Note: After the second call, the previous
"device-6d6f6sa66d"
entry is removed. Only the new entry remains.
Behavior on Error
Error Condition | Message Example |
---|---|
Missing arguments | ERR: InvalidArgsError: Key mapKey, and value must be provided |
Invalid key format | ERR: InvalidKeyError: <details> |
Invalid mapKey (empty/invalid string) | ERR: InvalidMapKeyError: <details> |
Internal failure during map storage | ERR: <internal error message> |
Internals (for developers)
-
The old map stored at
<key>
is deleted. -
A new map object is created with
{<mapKey>: <value>}