Skip to main content

Description

Returns the number of keys that exist from those specified as arguments.

Method Signature

exists(...keys: string[]): Promise<number>

Parameters

keys
string[]
required
One or more keys to check for existence

Return Value

result
number
The number of keys that exist. If the same existing key is mentioned in the arguments multiple times, it will be counted multiple times.

Examples

Check single key existence

await redis.set("mykey", "value");
const exists = await redis.exists("mykey");
console.log(exists); // 1

const notExists = await redis.exists("nonexistent");
console.log(notExists); // 0

Check multiple keys

await redis.set("key1", "value1");
await redis.set("key2", "value2");

const exists = await redis.exists("key1", "key2", "key3");
console.log(exists); // 2 (key1 and key2 exist, key3 does not)

Duplicate keys are counted multiple times

await redis.set("mykey", "value");
const exists = await redis.exists("mykey", "mykey", "mykey");
console.log(exists); // 3

See Also

  • del - Delete keys
  • keys - Find keys matching a pattern