HVALS

HVALS key
Available since:
Redis Open Source 2.0.0
Time complexity:
O(N) where N is the size of the hash.
ACL categories:
@read, @hash, @slow,
Compatibility:
Redis Software and Redis Cloud compatibility

Returns all values in the hash stored at key.

Required arguments

key

The name of the key that holds the hash.

Examples

Foundational: Retrieve all values from a hash using HVALS (returns only values without field names, useful when you only need the data)
redis> HSET myhash field1 "Hello"
(integer) 1
redis> HSET myhash field2 "World"
(integer) 1
redis> HVALS myhash
1) "Hello"
2) "World"
res10 = r.hset("myhash", mapping={"field1": "Hello", "field2": "World"})

res11 = r.hvals("myhash")
print(res11) # >>> [ "Hello", "World" ]

const res12 = await client.hSet(
  'myhash',
  {
    'field1': 'Hello',
    'field2': 'World'
  }
)

const res13 = await client.hVals('myhash')
console.log(res13) // [ 'Hello', 'World' ]


import assert from 'node:assert';
import { Redis } from 'ioredis';

const redis = new Redis();


await redis.hset('myhash', { field1: 'Hello', field2: 'World' });

const hmgetResult = await redis.hmget('myhash', 'field1', 'field2', 'nofield');
console.log(hmgetResult); // >>> ['Hello', 'World', null]


redis.disconnect();

        Map<String, String> hValsExampleParams = new HashMap<>();
        hValsExampleParams.put("field1", "Hello");
        hValsExampleParams.put("field2", "World");

        long hValsResult1 = jedis.hset("myhash", hValsExampleParams);
        System.out.println(hValsResult1); // >>> 2

        List<String> hValsResult2 = jedis.hvals("myhash");
        Collections.sort(hValsResult2);
        System.out.println(hValsResult2);
        // >>> [Hello, World]
            Map<String, String> hValsExampleParams = new HashMap<>();
            hValsExampleParams.put("field1", "Hello");
            hValsExampleParams.put("field2", "World");

            CompletableFuture<Void> hValsExample = asyncCommands.hset("myhash", hValsExampleParams).thenCompose(res1 -> {
                return asyncCommands.hvals("myhash");
            }).thenAccept(res2 -> {
                List<String> sortedValues = new ArrayList<>(res2);
                Collections.sort(sortedValues);
                System.out.println(sortedValues);
                // >>> [Hello, World]
            }).toCompletableFuture();
            Map<String, String> hValsExampleParams = new HashMap<>();
            hValsExampleParams.put("field1", "Hello");
            hValsExampleParams.put("field2", "World");

            Mono<Long> hValsExample1 = reactiveCommands.hset("myhash", hValsExampleParams).doOnNext(result -> {
            });

            hValsExample1.block();

            Mono<List<String>> hValsExample2 = reactiveCommands.hvals("myhash").collectList().doOnNext(result -> {
                List<String> sortedValues = new ArrayList<>(result);
                Collections.sort(sortedValues);
                System.out.println(sortedValues);
                // >>> [Hello, World]
            });
	hValsResult1, err := rdb.HSet(ctx, "myhash",
		"field1", "Hello",
		"field2", "World",
	).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(hValsResult1) // >>> 2

	hValsResult2, err := rdb.HVals(ctx, "myhash").Result()

	if err != nil {
		panic(err)
	}

	sort.Strings(hValsResult2)

	fmt.Println(hValsResult2) // >>> [Hello World]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis/hiredis.h>

int main(int argc, char **argv) {
    redisContext *c = redisConnect("127.0.0.1", 6379);

    if (c == NULL || c->err) {
        if (c) {
            printf("Connection error: %s\n", c->errstr);
            redisFree(c);
        } else {
            printf("Connection error: can't allocate redis context\n");
        }
        return 1;
    }


    redisReply *reply;

    // Set up hash with fields
    reply = redisCommand(c, "HSET %s %s %s %s %s",
        "myhash", "field1", "Hello", "field2", "World");
    freeReplyObject(reply);

    // Get multiple fields at once
    reply = redisCommand(c, "HMGET %s %s %s %s",
        "myhash", "field1", "field2", "nofield");

    printf("HMGET myhash field1 field2 nofield:\n");
    for (size_t i = 0; i < reply->elements; i++) {
        if (reply->element[i]->type == REDIS_REPLY_NIL) {
            printf("  [%zu]: null\n", i);
        } else {
            printf("  [%zu]: %s\n", i, reply->element[i]->str);
        }
    }
    // >>> [0]: Hello
    // >>> [1]: World
    // >>> [2]: null


    freeReplyObject(reply);


    redisFree(c);

    return 0;
}

        db.HashSet("myhash",
            [
                new("field1", "Hello"),
                new("field2", "World")
            ]
        );

        RedisValue[] hValsResult = db.HashValues("myhash");
        Array.Sort(hValsResult);
        Console.WriteLine(string.Join(", ", hValsResult));
        // >>> Hello, World

        $hValsResult1 = $this->redis->hmset('myhash', ['field1' => 'Hello', 'field2' => 'World']);
        echo "HMSET myhash field1 Hello field2 World: " . ($hValsResult1 ? 'OK' : 'FAIL') . "\n"; // >>> OK

        $hValsResult2 = $this->redis->hvals('myhash');
        echo "HVALS myhash: " . json_encode($hValsResult2) . "\n"; // >>> ["Hello","World"]
        let hash_fields = [
            ("field1", "Hello"),
            ("field2", "World"),
        ];

        if let Ok(_) = r.hset_multiple::<&str, &str, &str, String>("myhash", &hash_fields) {
            // Fields set successfully
        }

        match r.hvals("myhash") {
            Ok(res11) => {
                let res11: Vec<String> = res11;
                println!("{res11:?}");    // >>> ["Hello", "World"]
            },
            Err(e) => {
                println!("Error getting hash values: {e}");
                return;
            }
        }

        let hash_fields = [
            ("field1", "Hello"),
            ("field2", "World"),
        ];

        if let Ok(_) = r.hset_multiple::<&str, &str, &str, String>("myhash", &hash_fields).await {
            // Fields set successfully
        }

        match r.hvals("myhash").await {
            Ok(res11) => {
                let res11: Vec<String> = res11;
                println!("{res11:?}");    // >>> ["Hello", "World"]
            },
            Err(e) => {
                println!("Error getting hash values: {e}");
                return;
            }
        }

Give these commands a try in the interactive console:

HSET myhash field1 "Hello" HSET myhash field2 "World" HVALS myhash

Redis Software and Redis Cloud compatibility

Redis
Software
Redis
Cloud
Notes
✅ Standard
✅ Active-Active
✅ Standard
✅ Active-Active

Return information

Array reply: a list of values in the hash, or an empty list when the key does not exist
RATE THIS PAGE
Back to top ↑