从JSON数组中按索引获取元素

huangapple go评论49阅读模式
英文:

Redis: get element from JSON array by index

问题

我使用的包是redis。我使用以下命令将JSON数组存储在Redis中:

await redisClient.json.set('car.volume.tire', '$', []);
await redisClient.json.arrAppend('car.volume.tire', '$', { hello: 'there' });

但是当我想按索引检索元素时,我收到null

await redisClient.json.get('car.volume.tire[0]');

然而,以下返回完整的JSON数组:

await redisClient.json.get('car.volume.tire');

我做错了什么?

英文:

The package I'm using is redis. I used the following command to store a JSON array in Redis:

await redisClient.json.set('car.volume.tire', '$', []);
await redisClient.json.arrAppend('car.volume.tire', '$', { hello: 'there' });

But when I want to retrieve the element by index, I receive null:

await redisClient.json.get(`car.volume.tire[0]`);

However, the following returns the full JSON array:

await redisClient.json.get(`car.volume.tire`);

What am I doing wrong?

答案1

得分: 1

以下是您要翻译的内容:

尝试

```js
await redisClient.json.get('car.volume.tire', { path: ['[0]'] });

或者你可以像Guy Royse建议的那样将path作为一个字符串传递:

await redisClient.json.get('car.volume.tire', { path: '[0]' });

参见:"从Redis检索JSON文档"


完整示例

import dotenv from "dotenv";
import { createClient } from "redis";

dotenv.config();

const { REDIS_HOST, REDIS_PORT } = process.env;

const redisClient = createClient({
  socket: {
    host: REDIS_HOST,
    port: REDIS_PORT,
  },
});

redisClient.on("error", (err) => console.log("Redis服务器错误", err));

await redisClient.connect();

await redisClient.json.set("car.volume.tire", "$", []);
await redisClient.json.arrAppend("car.volume.tire", "$", { hello: "there" });

const found = await redisClient.json.get("car.volume.tire", { path: "[0]" });

console.log(found); // { hello: 'there' }

redisClient.disconnect();

<details>
<summary>英文:</summary>

Try:

```js
await redisClient.json.get(&#39;car.volume.tire&#39;, { path: [&#39;[0]&#39;] });

or you can pass path as just a string as Guy Royse suggested:

await redisClient.json.get(&#39;car.volume.tire&#39;, { path: &#39;[0]&#39; });

See: "Retrieving JSON Documents from Redis"


Full example

import dotenv from &quot;dotenv&quot;;
import { createClient } from &quot;redis&quot;;

dotenv.config();

const { REDIS_HOST, REDIS_PORT } = process.env;

const redisClient = createClient({
  socket: {
    host: REDIS_HOST,
    port: REDIS_PORT,
  },
});

redisClient.on(&quot;error&quot;, (err) =&gt; console.log(&quot;Redis Server Error&quot;, err));

await redisClient.connect();

await redisClient.json.set(&quot;car.volume.tire&quot;, &quot;$&quot;, []);
await redisClient.json.arrAppend(&quot;car.volume.tire&quot;, &quot;$&quot;, { hello: &quot;there&quot; });

const found = await redisClient.json.get(&quot;car.volume.tire&quot;, { path: &quot;[0]&quot; });

console.log(found); // { hello: &#39;there&#39; }

redisClient.disconnect();

huangapple
  • 本文由 发表于 2023年6月15日 01:48:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定