1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-10 11:43:01 +03:00
Files
node-redis/documentation/json/index.html
documentation-bot 09b3d3a2d9 Updates
2022-05-02 15:49:25 +00:00

42 lines
12 KiB
HTML

<!DOCTYPE html><html class="default"><head><meta charSet="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>@redis/json</title><meta name="description" content="Documentation for @redis/json"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script async src="assets/search.js" id="search-script"></script></head><body><script>document.body.classList.add(localStorage.getItem("tsd-theme") || "os")</script><header><div class="tsd-page-toolbar"><div class="container"><div class="table-wrap"><div class="table-cell" id="tsd-search" data-base="."><div class="field"><label for="tsd-search-field" class="tsd-widget search no-caption">Search</label><input type="text" id="tsd-search-field"/></div><ul class="results"><li class="state loading">Preparing search index...</li><li class="state failure">The search index is not available</li></ul><a href="index.html" class="title">@redis/json</a></div><div class="table-cell" id="tsd-widgets"><div id="tsd-filter"><a href="#" class="tsd-widget options no-caption" data-toggle="options">Options</a><div class="tsd-filter-group"><div class="tsd-select" id="tsd-filter-visibility"><span class="tsd-select-label">All</span><ul class="tsd-select-list"><li data-value="public">Public</li><li data-value="protected">Public/Protected</li><li data-value="private" class="selected">All</li></ul></div> <input type="checkbox" id="tsd-filter-inherited" checked/><label class="tsd-widget" for="tsd-filter-inherited">Inherited</label><input type="checkbox" id="tsd-filter-externals" checked/><label class="tsd-widget" for="tsd-filter-externals">Externals</label></div></div><a href="#" class="tsd-widget menu no-caption" data-toggle="menu">Menu</a></div></div></div></div><div class="tsd-page-title"><div class="container"><h1> @redis/json </h1></div></div></header><div class="container container-main"><div class="row"><div class="col-8 col-content"><div class="tsd-panel tsd-typography">
<a href="#redisjson" id="redisjson" style="color: inherit; text-decoration: none;">
<h1>@redis/json</h1>
</a>
<p>This package provides support for the <a href="https://redisjson.io">RedisJSON</a> module, which adds JSON as a native data type to Redis. It extends the <a href="https://github.com/redis/node-redis">Node Redis client</a> to include functions for each of the RedisJSON commands.</p>
<p>To use these extra commands, your Redis server must have the RedisJSON module installed.</p>
<a href="#usage" id="usage" style="color: inherit; text-decoration: none;">
<h2>Usage</h2>
</a>
<p>For a complete example, see <a href="https://github.com/redis/node-redis/blob/master/examples/managing-json.js"><code>managing-json.js</code></a> in the Node Redis examples folder.</p>
<a href="#storing-json-documents-in-redis" id="storing-json-documents-in-redis" style="color: inherit; text-decoration: none;">
<h3>Storing JSON Documents in Redis</h3>
</a>
<p>The <a href="https://oss.redis.com/redisjson/commands/#jsonset"><code>JSON.SET</code></a> command stores a JSON value at a given JSON Path in a Redis key.</p>
<p>Here, we&#39;ll store a JSON document in the root of the Redis key &quot;<code>mydoc</code>&quot;:</p>
<pre><code class="language-javascript"><span class="hl-0">import</span><span class="hl-1"> { </span><span class="hl-2">createClient</span><span class="hl-1"> } </span><span class="hl-0">from</span><span class="hl-1"> </span><span class="hl-3">&#39;redis&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-1">...</span><br/><span class="hl-0">await</span><span class="hl-1"> </span><span class="hl-2">client</span><span class="hl-1">.</span><span class="hl-2">json</span><span class="hl-1">.</span><span class="hl-4">set</span><span class="hl-1">(</span><span class="hl-3">&#39;noderedis:jsondata&#39;</span><span class="hl-1">, </span><span class="hl-3">&#39;$&#39;</span><span class="hl-1">, {</span><br/><span class="hl-1"> </span><span class="hl-2">name:</span><span class="hl-1"> </span><span class="hl-3">&#39;Roberta McDonald&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">pets:</span><span class="hl-1"> [</span><br/><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-2">name:</span><span class="hl-1"> </span><span class="hl-3">&#39;Rex&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">species:</span><span class="hl-1"> </span><span class="hl-3">&#39;dog&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">age:</span><span class="hl-1"> </span><span class="hl-5">3</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">isMammal:</span><span class="hl-1"> </span><span class="hl-6">true</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-2">name:</span><span class="hl-1"> </span><span class="hl-3">&#39;Goldie&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">species:</span><span class="hl-1"> </span><span class="hl-3">&#39;fish&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">age:</span><span class="hl-1"> </span><span class="hl-5">2</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">isMammal:</span><span class="hl-1"> </span><span class="hl-6">false</span><br/><span class="hl-1"> }</span><br/><span class="hl-1"> ]</span><br/><span class="hl-1">});</span>
</code></pre>
<p>For more information about RedisJSON&#39;s path syntax, <a href="https://oss.redis.com/redisjson/path/">check out the documentation</a>.</p>
<a href="#retrieving-json-documents-from-redis" id="retrieving-json-documents-from-redis" style="color: inherit; text-decoration: none;">
<h3>Retrieving JSON Documents from Redis</h3>
</a>
<p>With RedisJSON, we can retrieve all or part(s) of a JSON document using the <a href=""><code>JSON.GET</code></a> command and one or more JSON Paths. Let&#39;s get the name and age of one of the pets:</p>
<pre><code class="language-javascript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">results</span><span class="hl-1"> = </span><span class="hl-0">await</span><span class="hl-1"> </span><span class="hl-2">client</span><span class="hl-1">.</span><span class="hl-2">json</span><span class="hl-1">.</span><span class="hl-4">get</span><span class="hl-1">(</span><span class="hl-3">&#39;noderedis:jsondata&#39;</span><span class="hl-1">, {</span><br/><span class="hl-1"> </span><span class="hl-2">path:</span><span class="hl-1"> [</span><br/><span class="hl-1"> </span><span class="hl-3">&#39;.pets[1].name&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-3">&#39;.pets[1].age&#39;</span><br/><span class="hl-1"> ]</span><br/><span class="hl-1">});</span>
</code></pre>
<p><code>results</code> will contain the following:</p>
<pre><code class="language-javascript"><span class="hl-1"> { </span><span class="hl-3">&#39;.pets[1].name&#39;</span><span class="hl-1">: </span><span class="hl-3">&#39;Goldie&#39;</span><span class="hl-1">, </span><span class="hl-3">&#39;.pets[1].age&#39;</span><span class="hl-1">: </span><span class="hl-5">2</span><span class="hl-1"> }</span>
</code></pre>
<a href="#performing-atomic-updates-on-json-documents-stored-in-redis" id="performing-atomic-updates-on-json-documents-stored-in-redis" style="color: inherit; text-decoration: none;">
<h3>Performing Atomic Updates on JSON Documents Stored in Redis</h3>
</a>
<p>RedisJSON includes commands that can atomically update values in a JSON document, in place in Redis without having to first retrieve the entire document.</p>
<p>Using the <a href="https://oss.redis.com/redisjson/commands/#jsonnumincrby"><code>JSON.NUMINCRBY</code></a> command, we can update the age of one of the pets like this:</p>
<pre><code class="language-javascript"><span class="hl-0">await</span><span class="hl-1"> </span><span class="hl-2">client</span><span class="hl-1">.</span><span class="hl-2">json</span><span class="hl-1">.</span><span class="hl-4">numIncrBy</span><span class="hl-1">(</span><span class="hl-3">&#39;noderedis:jsondata&#39;</span><span class="hl-1">, </span><span class="hl-3">&#39;.pets[1].age&#39;</span><span class="hl-1">, </span><span class="hl-5">1</span><span class="hl-1">);</span>
</code></pre>
<p>And we can add a new object to the pets array with the <a href="https://oss.redis.com/redisjson/commands/#jsonarrappend"><code>JSON.ARRAPPEND</code></a> command:</p>
<pre><code class="language-javascript"><span class="hl-1"> </span><span class="hl-0">await</span><span class="hl-1"> </span><span class="hl-2">client</span><span class="hl-1">.</span><span class="hl-2">json</span><span class="hl-1">.</span><span class="hl-4">arrAppend</span><span class="hl-1">(</span><span class="hl-3">&#39;noderedis:jsondata&#39;</span><span class="hl-1">, </span><span class="hl-3">&#39;.pets&#39;</span><span class="hl-1">, {</span><br/><span class="hl-1"> </span><span class="hl-2">name:</span><span class="hl-1"> </span><span class="hl-3">&#39;Robin&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">species:</span><span class="hl-1"> </span><span class="hl-3">&#39;bird&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">age:</span><span class="hl-1"> </span><span class="hl-5">1</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">isMammal:</span><span class="hl-1"> </span><span class="hl-6">false</span><br/><span class="hl-1"> });</span>
</code></pre>
</div></div><div class="col-4 col-menu menu-sticky-wrap menu-highlight"><nav class="tsd-navigation primary"><ul><li class="current"><a href="modules.html">Modules</a></li><li class=" tsd-kind-module"><a href="modules/commands.html">commands</a></li><li class=" tsd-kind-module"><a href="modules/commands_ARRAPPEND.html">commands/ARRAPPEND</a></li><li class=" tsd-kind-module"><a href="modules/commands_ARRINDEX.html">commands/ARRINDEX</a></li><li class=" tsd-kind-module"><a href="modules/commands_ARRINSERT.html">commands/ARRINSERT</a></li><li class=" tsd-kind-module"><a href="modules/commands_ARRLEN.html">commands/ARRLEN</a></li><li class=" tsd-kind-module"><a href="modules/commands_ARRPOP.html">commands/ARRPOP</a></li><li class=" tsd-kind-module"><a href="modules/commands_ARRTRIM.html">commands/ARRTRIM</a></li><li class=" tsd-kind-module"><a href="modules/commands_DEBUG_MEMORY.html">commands/DEBUG_<wbr/>MEMORY</a></li><li class=" tsd-kind-module"><a href="modules/commands_DEL.html">commands/DEL</a></li><li class=" tsd-kind-module"><a href="modules/commands_FORGET.html">commands/FORGET</a></li><li class=" tsd-kind-module"><a href="modules/commands_GET.html">commands/GET</a></li><li class=" tsd-kind-module"><a href="modules/commands_MGET.html">commands/MGET</a></li><li class=" tsd-kind-module"><a href="modules/commands_NUMINCRBY.html">commands/NUMINCRBY</a></li><li class=" tsd-kind-module"><a href="modules/commands_NUMMULTBY.html">commands/NUMMULTBY</a></li><li class=" tsd-kind-module"><a href="modules/commands_OBJKEYS.html">commands/OBJKEYS</a></li><li class=" tsd-kind-module"><a href="modules/commands_OBJLEN.html">commands/OBJLEN</a></li><li class=" tsd-kind-module"><a href="modules/commands_RESP.html">commands/RESP</a></li><li class=" tsd-kind-module"><a href="modules/commands_SET.html">commands/SET</a></li><li class=" tsd-kind-module"><a href="modules/commands_STRAPPEND.html">commands/STRAPPEND</a></li><li class=" tsd-kind-module"><a href="modules/commands_STRLEN.html">commands/STRLEN</a></li><li class=" tsd-kind-module"><a href="modules/commands_TYPE.html">commands/TYPE</a></li><li class=" tsd-kind-module"><a href="modules/index.html">index</a></li></ul></nav></div></div></div><footer class="with-border-bottom"><div class="container"><h2>Legend</h2><div class="tsd-legend-group"><ul class="tsd-legend"><li class="tsd-kind-variable"><span class="tsd-kind-icon">Variable</span></li><li class="tsd-kind-function"><span class="tsd-kind-icon">Function</span></li><li class="tsd-kind-type-alias"><span class="tsd-kind-icon">Type alias</span></li></ul></div><h2>Settings</h2><p>Theme <select id="theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></p></div></footer><div class="container tsd-generator"><p>Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></div><div class="overlay"></div><script src="assets/main.js"></script></body></html>