EXISTS
The EXISTS
command in DiceDB is used to determine if one or more specified keys exist in the database. It returns the number of keys that exist among the specified ones.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The key(s) to check for existence. One or more keys can be specified, separated by spaces. | String | Yes |
Return values
Condition | Return Value |
---|---|
None of the specified keys exist | 0 |
One or more specified keys exist | Integer representing the count of keys that exist |
Behaviour
- The
EXISTS
command checks whether the specified keys are present in the database. - Returns 1 or 0, or for multiple keys returns the count of existing keys.
- The command performs a read-only operation and does not modify the database.
Errors
-
Wrong number of arguments
:- Error Message:
(error) ERR wrong number of arguments for 'exists' command
- Occurs when no key is provided.
- Error Message:
-
Wrong type of value or key
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs when attempting to use the command on a key that contains a non-string value.
- Error Message:
Example Usage
Single Key Check
Checking if a key mykey
exists in the database:
Multiple Keys Check
Checking if multiple keys (key1
, key2
, key3
) exist in the database:
In this case, key1
and key2
exist, but key3
does not.
Non-Existent Key
Checking if a non-existent key (nonExistentKey
) is present in the database:
All Non-Existent Keys
Checking if all non-existent keys return 0:
Empty Command
Providing no keys should trigger an error: