How to get a string value in redis datastore – Redis GET | MGET

1 month ago Lalit Bhagtani 0

In this tutorial, we will learn about how to get a string value stored at a key in redis datastore, by using redis GET and MGET command.

GET Command :-

The GET command is used to get a string value stored at a specified key. If key doesn’t exist, nil is returned and if key exists but value stored at the key is not a string, then error is returned. The syntax of redis GET command is as follows :-

Syntax :-

redis host:post> GET <key name>

Output :-

- (strings) reply, representing a string value stored at a key.
- (nil), if key does not exists.
- Error, if key exist and value stored at the key is not a string.

Example :-

Redis GET

MGET Command :-

The MGET command is used to get a string values stored at all the specified key. If any of the specified key doesn’t exist or if exist but value stored at the key is not a string, then nil is returned. The syntax of redis MGET command is as follows :-

Syntax :-

redis host:post> MGET <keyname-1> <keyname-2> <keyname-3>

Output :-

- (array) reply, representing a string values stored at all the specified key.
- (nil), if key does not exists.
- (nil), if key exist and value stored at the key is not a string.

Example :-

Redis MGET

References :-

  1. GET Command Docs
  2. MGET Command Docs

That’s all for how to get a string value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.