Hash

HRANDFIELD

Return a random field from a hash

Arguments

keybodystringrequired

The key of the hash.

countbodyinteger

Optionally return more than one field.

withValuesbodyboolean

Return the values of the fields as well.

Response

Record<string, unknown>required

An object containing the fields and their values.

Basic
await redis.hset("key", {  id: 1,  username: "chronark",  name: "andreas" });const randomField = await redis.hrandfield("key");console.log(randomField); // one of "id", "username" or  "name"
Multiple Fields
await redis.hset("key", {  id: 1,  username: "chronark",  name: "andreas",});const randomFields = await redis.hrandfield("key", 2);console.log(randomFields); // ["id", "username"] or any other combination
With Values
await redis.hset("key", {  id: 1,  username: "chronark",  name: "andreas",});const randomFields = await redis.hrandfield("key", 2, true);console.log(randomFields); // { id: "1", username: "chronark" } or any other combination
Loading search…