HMGET

HMGET key field [field ...]
Available since:
Redis Open Source 2.0.0
Time complexity:
O(N) where N is the number of fields being requested.
ACL categories:
@read, @hash, @fast,
Compatibility:
Redis Software and Redis Cloud compatibility

Returns the values associated with the specified fields in the hash stored at key.

For every field that does not exist in the hash, a nil value is returned. Because non-existing keys are treated as empty hashes, running HMGET against a non-existing key will return a list of nil values.

Required arguments

key

The name of the key that holds the hash.

field [field ...]

One or more fields whose values to retrieve.

Examples

Foundational: Retrieve multiple field values from a hash using HMGET (returns nil if field or key doesn't exist)
> HSET myhash field1 "Hello"
(integer) 1
> HSET myhash field2 "World"
(integer) 1
> HMGET myhash field1 field2 nofield
1) "Hello"
2) "World"
3) (nil)
>
r.hset("myhash", mapping={"field1": "Hello", "field2": "World"})

hmget_result = r.hmget("myhash", ["field1", "field2", "nofield"])
print(hmget_result)  # >>> ['Hello', 'World', None]

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

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

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

const hmgetResult = await redis.hmget('myhash', 'field1', 'field2', 'nofield');
console.log(hmgetResult); // >>> ['Hello', 'World', null]
        Map<String, String> hmgetExampleParams = new HashMap<>();
        hmgetExampleParams.put("field1", "Hello");
        hmgetExampleParams.put("field2", "World");

        jedis.hset("myhash", hmgetExampleParams);

        List<String> hmgetResult = jedis.hmget("myhash", "field1", "field2", "nofield");
        System.out.println(hmgetResult);    // >>> [Hello, World, null]
            Map<String, String> hmgetExampleParams = new HashMap<>();
            hmgetExampleParams.put("field1", "Hello");
            hmgetExampleParams.put("field2", "World");

            CompletableFuture<Void> hmgetExample = asyncCommands.hset("myhash", hmgetExampleParams).thenCompose(res1 -> {
                return asyncCommands.hmget("myhash", "field1", "field2", "nofield");
            }).thenAccept(res2 -> {
                System.out.println(res2); // >>> [KeyValue[field1, Hello], KeyValue[field2, World], KeyValue[nofield, null]]
            }).toCompletableFuture();
            Map<String, String> hmgetExampleParams = new HashMap<>();
            hmgetExampleParams.put("field1", "Hello");
            hmgetExampleParams.put("field2", "World");

            Mono<Long> hmgetExample1 = reactiveCommands.hset("myhash", hmgetExampleParams);

            hmgetExample1.block();

            Flux<KeyValue<String, String>> hmgetExample2 = reactiveCommands.hmget("myhash", "field1", "field2", "nofield")
                    .doOnNext(result -> {
                        System.out.println(result);
                        // >>> KeyValue[field1, Hello]
                        // >>> KeyValue[field2, World]
                        // >>> KeyValue[nofield, null]
                    });
	rdb.HSet(ctx, "myhash", "field1", "Hello", "field2", "World")

	hmgetResult, err := rdb.HMGet(ctx, "myhash", "field1", "field2", "nofield").Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(hmgetResult) // >>> [Hello World <nil>]
    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
        db.HashSet("myhash",
            [
                new("field1", "Hello"),
                new("field2", "World")
            ]
        );

        RedisValue[] hmgetResult = db.HashGet("myhash", new RedisValue[] { "field1", "field2", "nofield" });
        Console.WriteLine(string.Join(", ", hmgetResult.Select(v => v.IsNull ? "null" : v.ToString())));
        // >>> Hello, World, null
        $this->redis->hmset('myhash', ['field1' => 'Hello', 'field2' => 'World']);

        $hmgetResult = $this->redis->hmget('myhash', ['field1', 'field2', 'nofield']);
        echo "HMGET myhash field1 field2 nofield: ";
        print_r($hmgetResult);
        // >>> Array ( [0] => Hello [1] => World [2] => )
        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.hmget::<&str, &[&str], Vec<Option<String>>>("myhash", &["field1", "field2", "nofield"]) {
            Ok(hmget_result) => {
                println!("{:?}", hmget_result);    // >>> [Some("Hello"), Some("World"), None]
            },
            Err(e) => {
                println!("Error getting hash fields: {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.hmget::<&str, &[&str], Vec<Option<String>>>("myhash", &["field1", "field2", "nofield"]).await {
            Ok(hmget_result) => {
                println!("{:?}", hmget_result);    // >>> [Some("Hello"), Some("World"), None]
            },
            Err(e) => {
                println!("Error getting hash fields: {e}");
                return;
            }
        }

Give these commands a try in the interactive console:

HSET myhash field1 "Hello" HSET myhash field2 "World" HMGET myhash field1 field2 nofield

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 associated with the given fields, in the same order as they are requested.
RATE THIS PAGE
Back to top ↑