在Redis上存储JSON数组
Store JSON array on Redis
我在javascript中有以下JSON数组:
[
{
title: 'Title 1',
description: 'Description 1',
startTime: 'Start time 1',
duration: 5,
humidity: 50
},
{
title: 'Title 2',
description: 'Description 2',
startTime: 'Start time 2',
duration: 15,
humidity: 30
},
{
title: 'Title 2',
description: 'Description 2',
startTime: 'Start time 2',
duration: 10,
humidity: 40
}
]
我需要将其存储在Redis存储上。我需要能够在大多数情况下检索整个数组,但有时我需要获得特定的项。在SQL中,我可以创建一个自动递增的"ID",并将每个条目存储在一行中。但是用Redis实现这一目标的最佳方法是什么呢?
谢谢!
我在Redis中保存相同的结构。当您需要使用RedisJSON检索JSON格式的所有数据时,这是优化的。例如保存结构:
const tagsArray = {
id: '101',
qc: 3,
ts: '2022-07-07T14:36:14.712Z',
val: 10.332507886284837,
tag: 'DB_right.Corrente'
},
{ ... },
{ ... }
}
我正在使用代码(例如:Node.js with redis npm module):
const redisClient = createClient()
await redisClient.connect()
await redisClient.json.set('arr:key', '$', [])
await redisClient.json.arrAppend('arr:key', '$', {searchKey: tagsArray})
const r = await redisClient.json.get('arr:key')
console.log('@R >> ', r)
您可以为JSON数组中的每个元素构建一个searchKey,您将用项目填充!
相关文章:
- 需要帮助设置json数组
- 如何使用 node.js 比较两个 json 数组
- 根据id将json数组组合为一个json数组
- 如何创建JSON数组
- 如何将package.json数组传递给grunt.js
- 访问json数组中的对象
- JSON数组数据返回Undefined
- 如何向JSON数组动态添加属性
- 如何从json数组中获取特定值
- 从多维嵌套json数组创建下拉列表
- 在ListView中添加JSON数组中存储的图像-ReactNative
- jquery在json数组中循环
- 将 JSON 数组解析为 JavaScript 数组
- 如何在php中读取没有任何键的JSON数组
- 如何在Jquery中迭代JSON数组
- 如何在cycle js中从JSON数组创建组件
- 如何使用jquery返回php-json数组对象
- JSON数组转换为JS对象数组
- 使用System从C#集合创建JSON数组.网状物剧本序列化
- 使用Underscore.js修改json数组中所选元素的更有效方法