javascript - 是否有任何正常的解决方案来编辑列表 Aerospike 中的地图
问题描述
我试图用这个变体编辑列表中的对象
const { bins: data } = await client.get(key); // { array: [{ variable: 1 }, { variable: 2 }] }
const { array } = await client.operate(key, [Aerospike.maps.put('array', 3).withContext(ctx => ctx.addListIndex(1).addMapKey('variable'))]); // want to edit second object in array, but i get an error 'Operations array invalid'
我可以正常做还是唯一的方法是按索引删除对象并插入新对象?
解决方案
是的,您可以使用 Aerospike 的 Map 操作来更新嵌套的地图值!
operate
您的命令有两个问题:该maps.put
操作需要 3 个参数:bin 名称(array
在您的情况下)、映射键(variable
)和新值(3
)。此操作的上下文只是第二个列表对象 - 也不需要将映射键添加为上下文。
这是一个完整的工作示例:
// nested-cdt-ops.js
const as = require('aerospike')
as.connect().then(async (client) => {
const key = new as.Key('test', 'test', 'nested')
{
const bins = { array: [{ variable: 1 }, { variable: 2 }] }
await client.put(key, bins)
console.log('BEFORE:', bins)
}
const operations = [
as.maps.put('array', 'variable', 3).withContext(
(ctx) => ctx.addListIndex(1)
)
]
await client.operate(key, operations)
{
const { bins } = await client.get(key)
console.log('AFTER:', bins)
}
client.close()
}).catch((error) => {
if (error.client) error.client.close()
console.error(error)
})
它输出:
$ node nested-cdt-ops.js
BEFORE: { array: [ { variable: 1 }, { variable: 2 } ] }
AFTER: { array: [ { variable: 1 }, { variable: 3 } ] }
推荐阅读
- cron - Airflow schedular 不会在特定 schedule_interval 配置中选择 DAG(0 8 * * 5)
- asp.net - 激活 ILogManager 时出错:没有可用的匹配绑定,并且类型不可自绑定
- grails - 将 obj 和数量列表放入 commnad obj 中
- git - 使用 Git 过滤分支时添加注释以提交
- unity3d - 通过透视相机投影正交矩阵
- google-cloud-platform - Google Cloud Scheduler - 不尊重 HTTP 标头
- logstash - 用流利的日志时间更新时间戳/时间键没有运气
- c# - 使用网络应用程序通过机器人主动发送消息
- sql - How to exclude a certain range in SQL results?
- rest - 微服务/REST - 如何在其他服务中存储对资源的引用