Class: MapOperation

aerospike/maps~ MapOperation

Use the methods in the maps module to create map operations for use with the Client#operate command.


new MapOperation()

Source:

Methods


andReturn(returnType)

Set the return type for certain map operations.

The return type only affects getBy* and removeBy* map operations.

Parameters:
Name Type Description
returnType number

The return type indicating what data of the selected items to return.

Source:
Example

Remove map keys in a given range and return the values

const Aerospike = require('aerospike')
const maps = Aerospike.maps
const key = new Aerospike.Key('test', 'demo', 'mapKey')

Aerospike.connect().then(async client => {
  await client.put(key, { map: { a: 1, b: 2, c: 3, d: 4, e: 5 } })
  const ops = [
    maps.removeByKeyRange('map', 'b', 'd')
      .andReturn(maps.returnType.VALUE)
  ]
  const result = await client.operate(key, ops)
  console.info('Result:', result.bins.map) // => Result: [ 2, 3 ]
  client.close()
})

withContext(context)

By setting the context, the map operation will be executed on a nested map, instead of the bin value itself.

Parameters:
Name Type Description
context CdtContext | function

Either a CdtContext object, or a function which accepts a CdtContext object.

Since:
  • v3.12.0
Source:
Returns:

The map operation itself.

Type
MapOperation
Examples

Fetch the value with the key 'b' from the 2nd nested map

const Aerospike = require('aerospike')
const maps = Aerospike.maps
const key = new Aerospike.Key('test', 'demo', 'mapsTest')

Aerospike.connect().then(async (client) => {
  await client.put(key, { maps: [{ a: 1, b: 2 }, { b: 3, c: 4 }] })
  const ops = [
    maps.getByKey('maps', 'b')
      .withContext((ctx) => ctx.addListIndex(1))
      .andReturn(maps.returnType.VALUE)
  ]
  const result = await client.operate(key, ops)
  console.info('Result:', result.bins.maps) // => Result: 3
  client.close()
})

Fetch the value with the key 'b' from the nested map under the key 'alpha'

const Aerospike = require('./')
const maps = Aerospike.maps
const Context = Aerospike.cdt.Context
const key = new Aerospike.Key('test', 'demo', 'listsTest')

Aerospike.connect().then(async (client) => {
  await client.put(key, { maps: { alpha: { a: 1, b: 2 }, beta: { b: 3, c: 4 } } })
  const context = new Context().addMapKey('alpha')
  const ops = [
    maps.getByKey('maps', 'b')
      .withContext(context)
      .andReturn(maps.returnType.VALUE)
  ]
  const result = await client.operate(key, ops)
  console.info('Result:', result.bins.maps) // => Result: 2
  client.close()
})