MGET

MGET key [key ...]
Available since:
Redis Open Source 1.0.0
Time complexity:
O(N) where N is the number of keys to retrieve.
ACL categories:
@read, @string, @fast,
Compatibility:
Redis Software and Redis Cloud compatibility
Note:
This command's behavior varies in clustered Redis environments. See the multi-key operations page for more information.

Returns the values of all specified keys. For every key that does not hold a string value or does not exist, nil is returned. Because of this, the operation never fails.

Required arguments

key [key ...]

One or more keys whose values to retrieve.

Examples

Returns the values of all specified keys.
> SET key1 "Hello"
"OK"
> SET key2 "World"
"OK"
> MGET key1 key2 nonexisting
1) "Hello"
2) "World"
3) (nil)
r.set("key1", "Hello")
r.set("key2", "World")

mget_result = r.mget("key1", "key2", "nonexisting")
print(mget_result)
# >>> ['Hello', 'World', None]
await client.set('key1', 'Hello');
await client.set('key2', 'World');

const mgetResult = await client.mGet(['key1', 'key2', 'nonexisting']);
console.log(mgetResult); // >>> [ 'Hello', 'World', null ]
await redis.set('key1', 'Hello');
await redis.set('key2', 'World');

const mgetResult = await redis.mget('key1', 'key2', 'nonexisting');
console.log(mgetResult); // >>> ['Hello', 'World', null]
        String mgetResult1 = jedis.set("key1", "Hello");
        System.out.println(mgetResult1);    // >>> OK

        String mgetResult2 = jedis.set("key2", "World");
        System.out.println(mgetResult2);    // >>> OK

        java.util.List<String> mgetResult3 = jedis.mget("key1", "key2", "nonexisting");
        System.out.println(mgetResult3);    // >>> [Hello, World, null]
            CompletableFuture<Void> mgetExample = asyncCommands.set("key1", "Hello")
                    .thenCompose(res1 -> asyncCommands.set("key2", "World"))
                    .thenCompose(res2 -> asyncCommands.mget("key1", "key2", "nonexisting"))
                    .thenAccept(res3 -> {
                        System.out.println(res3);
                        // >>> [KeyValue[key1, Hello], KeyValue[key2, World], KeyValue[nonexisting, null]]
                    })
                    .toCompletableFuture();
            Mono<Void> mgetExample = reactiveCommands.set("key1", "Hello")
                    .flatMap(res1 -> reactiveCommands.set("key2", "World"))
                    .flatMap(res2 -> reactiveCommands.mget("key1", "key2", "nonexisting").collectList())
                    .doOnNext(res3 -> {
                        System.out.println(res3);
                        // >>> [KeyValue[key1, Hello], KeyValue[key2, World], KeyValue[nonexisting, null]]
                    })
                    .then();
	if err := rdb.Set(ctx, "key1", "Hello", 0).Err(); err != nil {
		panic(err)
	}

	if err := rdb.Set(ctx, "key2", "World", 0).Err(); err != nil {
		panic(err)
	}

	mgetResult, err := rdb.MGet(ctx, "key1", "key2", "nonexisting").Result()
	if err != nil {
		panic(err)
	}

	fmt.Println(mgetResult) // >>> [Hello World <nil>]
    redisReply *reply = redisCommand(c, "SET key1 Hello");
    freeReplyObject(reply);

    reply = redisCommand(c, "SET key2 World");
    freeReplyObject(reply);

    reply = redisCommand(c, "MGET key1 key2 nonexisting");

    for (size_t i = 0; i < reply->elements; i++) {
        if (i > 0) {
            printf(", ");
        }

        if (reply->element[i]->type == REDIS_REPLY_NIL) {
            printf("null");
        } else {
            printf("%s", reply->element[i]->str);
        }
    }
    printf("\n");
    // >>> Hello, World, null
        bool mgetResult1 = db.StringSet("key1", "Hello");
        bool mgetResult2 = db.StringSet("key2", "World");

        RedisValue[] mgetResult3 = db.StringGet(new RedisKey[] { "key1", "key2", "nonexisting" });
        Console.WriteLine(string.Join(", ", mgetResult3.Select(v => v.IsNull ? "null" : v.ToString())));
        // >>> Hello, World, null

        $r->set('key1', 'Hello');
        $r->set('key2', 'World');

        $mgetResult = $r->mget(['key1', 'key2', 'nonexisting']);
        echo json_encode($mgetResult) . PHP_EOL; // >>> ["Hello","World",null]
        if let Ok(res) = r.set("key1", "Hello") {
            let _: String = res;
        }

        if let Ok(res) = r.set("key2", "World") {
            let _: String = res;
        }

        match r.mget(&["key1", "key2", "nonexisting"]) {
            Ok(mget_result) => {
                let mget_result: Vec<Option<String>> = mget_result;
                println!("{mget_result:?}");    // >>> [Some("Hello"), Some("World"), None]
            }
            Err(e) => {
                println!("Error getting values: {e}");
            }
        }
        if let Ok(res) = r.set("key1", "Hello").await {
            let _: String = res;
        }

        if let Ok(res) = r.set("key2", "World").await {
            let _: String = res;
        }

        match r.mget(&["key1", "key2", "nonexisting"]).await {
            Ok(mget_result) => {
                let mget_result: Vec<Option<String>> = mget_result;
                println!("{mget_result:?}");    // >>> [Some("Hello"), Some("World"), None]
            }
            Err(e) => {
                println!("Error getting values: {e}");
            }
        }

Give these commands a try in the interactive console:

SET key1 "Hello" SET key2 "World" MGET key1 key2 nonexisting

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 at the specified keys.
RATE THIS PAGE
Back to top ↑