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

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

Redis: get element from JSON array by index

问题

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

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

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

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

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

  1. 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:

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

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

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

However, the following returns the full JSON array:

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

What am I doing wrong?

答案1

得分: 1

以下是您要翻译的内容:

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

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

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

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


完整示例

  1. import dotenv from "dotenv";
  2. import { createClient } from "redis";
  3. dotenv.config();
  4. const { REDIS_HOST, REDIS_PORT } = process.env;
  5. const redisClient = createClient({
  6. socket: {
  7. host: REDIS_HOST,
  8. port: REDIS_PORT,
  9. },
  10. });
  11. redisClient.on("error", (err) => console.log("Redis服务器错误", err));
  12. await redisClient.connect();
  13. await redisClient.json.set("car.volume.tire", "$", []);
  14. await redisClient.json.arrAppend("car.volume.tire", "$", { hello: "there" });
  15. const found = await redisClient.json.get("car.volume.tire", { path: "[0]" });
  16. console.log(found); // { hello: 'there' }
  17. redisClient.disconnect();
  1. <details>
  2. <summary>英文:</summary>
  3. Try:
  4. ```js
  5. 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:

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

See: "Retrieving JSON Documents from Redis"


Full example

  1. import dotenv from &quot;dotenv&quot;;
  2. import { createClient } from &quot;redis&quot;;
  3. dotenv.config();
  4. const { REDIS_HOST, REDIS_PORT } = process.env;
  5. const redisClient = createClient({
  6. socket: {
  7. host: REDIS_HOST,
  8. port: REDIS_PORT,
  9. },
  10. });
  11. redisClient.on(&quot;error&quot;, (err) =&gt; console.log(&quot;Redis Server Error&quot;, err));
  12. await redisClient.connect();
  13. await redisClient.json.set(&quot;car.volume.tire&quot;, &quot;$&quot;, []);
  14. await redisClient.json.arrAppend(&quot;car.volume.tire&quot;, &quot;$&quot;, { hello: &quot;there&quot; });
  15. const found = await redisClient.json.get(&quot;car.volume.tire&quot;, { path: &quot;[0]&quot; });
  16. console.log(found); // { hello: &#39;there&#39; }
  17. 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:

确定