首页 > 解决方案 > 是否有任何正常的解决方案来编辑​​列表 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'

我可以正常做还是唯一的方法是按索引删除对象并插入新对象?

标签: javascriptnode.jsaerospike

解决方案


是的,您可以使用 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 } ] }

推荐阅读