HLEN

HLEN key
Available since:
Redis Open Source 2.0.0
Time complexity:
O(1)
ACL categories:
@read, @hash, @fast,
Compatibility:
Redis Software and Redis Cloud compatibility

Returns the number of fields contained in the hash stored at key.

Required arguments

key

The name of the key that holds the hash.

Examples

Foundational: Count the fields in a hash with HLEN when you need a hash's size without transferring its contents
HSET myhash field1 "Hello" HSET myhash field2 "World" HLEN myhash
res15 = r.hset("myhash", "field1", "Hello")
print(res15)
# >>> 1

res16 = r.hset("myhash", "field2", "World")
print(res16)
# >>> 1

res17 = r.hlen("myhash")
print(res17)
# >>> 2

const res17 = await client.hSet('myhash', 'field1', 'Hello')
console.log(res17) // 1

const res18 = await client.hSet('myhash', 'field2', 'World')
console.log(res18) // 1

const res19 = await client.hLen('myhash')
console.log(res19) // 2

const hlenSet1 = await redis.hset('myhash', 'field1', 'Hello');
console.log(hlenSet1); // >>> 1

const hlenSet2 = await redis.hset('myhash', 'field2', 'World');
console.log(hlenSet2); // >>> 1

const hlenResult = await redis.hlen('myhash');
console.log(hlenResult); // >>> 2
        // `hset` returns 1 because `field1` is a new field.
        long hLenResult1 = jedis.hset("myhash", "field1", "Hello");
        System.out.println(hLenResult1);    // >>> 1

        // `hset` returns 1 because `field2` is also a new field.
        long hLenResult2 = jedis.hset("myhash", "field2", "World");
        System.out.println(hLenResult2);    // >>> 1

        long hLenResult3 = jedis.hlen("myhash");
        System.out.println(hLenResult3);    // >>> 2
            // `hset` returns true because `field1` is a new field.
            boolean res19 = syncCommands.hset("myhash", "field1", "Hello");
            System.out.println(res19); // >>> true

            // `hset` returns true because `field2` is also a new field.
            boolean res20 = syncCommands.hset("myhash", "field2", "World");
            System.out.println(res20); // >>> true

            Long res21 = syncCommands.hlen("myhash");
            System.out.println(res21); // >>> 2
            CompletableFuture<Void> hLenExample = asyncCommands.hset("myhash", "field1", "Hello").thenCompose(res1 -> {
                // `hset` returns true because `field1` is a new field.
                System.out.println(res1); // >>> true
                return asyncCommands.hset("myhash", "field2", "World");
            }).thenCompose(res2 -> {
                // `hset` returns true because `field2` is also a new field.
                System.out.println(res2); // >>> true
                return asyncCommands.hlen("myhash");
            }).thenAccept(res3 -> {
                System.out.println(res3); // >>> 2
            }).toCompletableFuture();
            Mono<Boolean> hLenExample1 = reactiveCommands.hset("myhash", "field1", "Hello").doOnNext(result -> {
                System.out.println(result); // >>> true
            });

            hLenExample1.block();

            Mono<Boolean> hLenExample2 = reactiveCommands.hset("myhash", "field2", "World").doOnNext(result -> {
                System.out.println(result); // >>> true
            });

            hLenExample2.block();

            Mono<Long> hLenExample3 = reactiveCommands.hlen("myhash").doOnNext(result -> {
                System.out.println(result); // >>> 2
            });
	hlen1, err := rdb.HSet(ctx, "myhash", "field1", "Hello").Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(hlen1) // >>> 1

	hlen2, err := rdb.HSet(ctx, "myhash", "field2", "World").Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(hlen2) // >>> 1

	hlen3, err := rdb.HLen(ctx, "myhash").Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(hlen3) // >>> 2
    // Add two new fields to the hash
    reply = redisCommand(c, "HSET %s %s %s", "myhash", "field1", "Hello");
    printf("HSET myhash field1 Hello: %lld\n", reply->integer); // >>> 1
    freeReplyObject(reply);

    reply = redisCommand(c, "HSET %s %s %s", "myhash", "field2", "World");
    printf("HSET myhash field2 World: %lld\n", reply->integer); // >>> 1
    freeReplyObject(reply);

    // Count the fields in the hash
    reply = redisCommand(c, "HLEN %s", "myhash");
    printf("HLEN myhash: %lld\n", reply->integer); // >>> 2
    freeReplyObject(reply);
        bool hlenRes1 = db.HashSet("myhash", "field1", "Hello");
        Console.WriteLine(hlenRes1);    // >>> True

        bool hlenRes2 = db.HashSet("myhash", "field2", "World");
        Console.WriteLine(hlenRes2);    // >>> True

        long hlenRes3 = db.HashLength("myhash");
        Console.WriteLine(hlenRes3);    // >>> 2

        $hLenResult1 = $this->redis->hset('myhash', 'field1', 'Hello');
        echo "HSET myhash field1 Hello: " . $hLenResult1 . "\n"; // >>> 1

        $hLenResult2 = $this->redis->hset('myhash', 'field2', 'World');
        echo "HSET myhash field2 World: " . $hLenResult2 . "\n"; // >>> 1

        $hLenResult3 = $this->redis->hlen('myhash');
        echo "HLEN myhash: " . $hLenResult3 . "\n"; // >>> 2
res19 = r.hset('myhash', 'field1', 'Hello')
puts res19 # >>> 1

res20 = r.hset('myhash', 'field2', 'World')
puts res20 # >>> 1

res21 = r.hlen('myhash')
puts res21 # >>> 2
        match r.hset("myhash", "field1", "Hello") {
            Ok(hlen1) => {
                let hlen1: i32 = hlen1;
                println!("{hlen1}");    // >>> 1
            },
            Err(e) => {
                println!("Error setting hash field: {e}");
                return;
            }
        }

        match r.hset("myhash", "field2", "World") {
            Ok(hlen2) => {
                let hlen2: i32 = hlen2;
                println!("{hlen2}");    // >>> 1
            },
            Err(e) => {
                println!("Error setting hash field: {e}");
                return;
            }
        }

        match r.hlen("myhash") {
            Ok(hlen3) => {
                let hlen3: usize = hlen3;
                println!("{hlen3}");    // >>> 2
            },
            Err(e) => {
                println!("Error getting hash length: {e}");
                return;
            }
        }

        match r.hset("myhash", "field1", "Hello").await {
            Ok(res15) => {
                let res15: i32 = res15;
                println!("{res15}");    // >>> 1
            },
            Err(e) => {
                println!("Error setting hash field: {e}");
                return;
            }
        }

        match r.hset("myhash", "field2", "World").await {
            Ok(res16) => {
                let res16: i32 = res16;
                println!("{res16}");    // >>> 1
            },
            Err(e) => {
                println!("Error setting hash field: {e}");
                return;
            }
        }

        match r.hlen("myhash").await {
            Ok(res17) => {
                let res17: usize = res17;
                println!("{res17}");    // >>> 2
            },
            Err(e) => {
                println!("Error getting hash length: {e}");
                return;
            }
        }

Redis Software and Redis Cloud compatibility

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

Return information

Integer reply: the number of fields in the hash, or 0 when the key does not exist.
RATE THIS PAGE
Back to top ↑