Back to Redis

JSON.MSET

content/commands/json.mset.md

latest3.3 KB
Original Source

{{< note >}} This command's behavior varies in clustered Redis environments. See the [multi-key operations]({{< relref "/develop/using-commands/multi-key-operations" >}}) page for more information. {{< /note >}}

Set or update one or more JSON values according to the specified key-path-value triplets

JSON.MSET is atomic, hence, all given additions or updates are either applied or not. It is not possible for clients to see that some of the keys were updated while others are unchanged.

A JSON value is a hierarchical structure. If you change a value in a specific path - nested values are affected.

{{% warning %}} When cluster mode is enabled, all specified keys must reside on the same hash slot. {{% /warning %}}

Examples

Required arguments

<details open><summary><code>key</code></summary>

is key to modify.

</details> <details open><summary><code>path</code></summary>

is JSONPath to specify. For new Redis keys the path must be the root. For existing keys, when the entire path exists, the value that it contains is replaced with the json value. For existing keys, when the path exists, except for the last element, a new child is added with the json value.

</details> <details open><summary><code>value</code></summary>

is value to set at the specified path

</details>

Examples

<details open> <summary><b>Add a new values in multiple keys</b></summary>

{{< highlight bash >}} redis> JSON.MSET doc1 $ '{"a":1}' doc2 $ '{"f":{"a":2}}' doc3 $ '{"f1":{"a":0},"f2":{"a":0}}' OK redis> JSON.MSET doc1 $ '{"a":2}' doc2 $.f.a '3' doc3 $ '{"f1":{"a":1},"f2":{"a":2}}' OK redis> JSON.GET doc1 $ "[{"a":2}]" redis> JSON.GET doc2 $ "[{"f":{"a":3}}]" redis> JSON.GET doc3 "{"f1":{"a":1},"f2":{"a":2}}" {{< / highlight >}}

</details>

Redis Software and Redis Cloud compatibility

| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Not supported">❌ Standard</span> <span title="Not supported"><nobr>❌ Active-Active</nobr></span> | <span title="Not supported">❌ Standard</span> <span title="Not supported"><nobr>❌ Active-Active</nobr></span> | |

Return information

{{< multitabs id="json-mset-return-info" tab1="RESP2" tab2="RESP3" >}}

One of the following:

  • [Simple string reply]({{< relref "/develop/reference/protocol-spec#simple-strings" >}}): OK if executed correctly.
  • [Simple error reply]({{< relref "/develop/reference/protocol-spec#simple-errors" >}}): if the operation fails to set the new values.

-tab-sep-

One of the following:

  • [Simple string reply]({{< relref "/develop/reference/protocol-spec#simple-strings" >}}): OK if executed correctly.
  • [Simple error reply]({{< relref "/develop/reference/protocol-spec#simple-errors" >}}): if the operation fails to set the new values.

{{< /multitabs >}}

See also

[JSON.SET]({{< relref "commands/json.set/" >}}) | [JSON.MGET]({{< relref "commands/json.mget/" >}}) | [JSON.GET]({{< relref "commands/json.get/" >}})

  • [RedisJSON]({{< relref "/develop/data-types/json/" >}})
  • [Index and search JSON documents]({{< relref "/develop/ai/search-and-query/indexing/" >}})