JSON.MGET
JSON.MGET
The JSON.MGET
command in DiceDB is used to retrieve the values of specific JSON keys from multiple JSON documents stored at different keys. This command is particularly useful when you need to fetch the same JSON path from multiple JSON objects in a single operation, thereby reducing the number of round trips to the DiceDB server.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | One or more keys from which the JSON values will be retrieved. These keys should point to JSON documents stored in DiceDB. | String | Yes |
path | The JSON path to retrieve from each of the specified keys. The path should be specified in JSONPath format. | String | Yes |
Return values
Condition | Return Value |
---|---|
Command is successful | An array of JSON values corresponding to the specified path from each of the provided keys. |
If a key does not exist or the path does not exist within a JSON document | The corresponding entry in the returned array will be nil . |
Behaviour
When the JSON.MGET
command is executed, DiceDB will:
- Iterate over each provided key.
- Retrieve the JSON document stored at each key.
- Extract the value at the specified JSON path from each document.
- Return an array of the extracted values.
If a key does not exist or the specified path is not found within a JSON document, nil
will be returned for that key.
Here’s the revised error section for the JSON.MGET
command documentation in the specified format:
Errors
-
Key does not exist
:- Error Message:
(error) ERROR could not perform this operation on a key that doesn't exist
- This error occurs when the specified key does not exist in the DiceDB database.
- Error Message:
-
Invalid JSON path
:- Error Message:
(error) ERROR invalid JSONPath
- This error occurs when the provided JSONPath expression is not valid.
- Error Message:
Example Usage
Basic Usage
Assume we have the following JSON documents stored in DiceDB:
To retrieve the name
field from each of these JSON documents:
Handling Non-Existent Keys and Paths
Assume we have the following JSON documents stored in DiceDB:
To retrieve the address
field, which does not exist in the documents:
In this case, user:3
does not exist, and the address
field does not exist in user:1
and user:2
, so nil
is returned for each key.