ListOperation

aerospike/lists~ ListOperation

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

Constructor

new ListOperation()

Source:

Methods

andReturn(returnType)

Set the return type for certain list operations.

Description:
  • The return type only affects getBy* and removeBy* list operations.

Source:
Example

Fetch the first three list elements and return the values

const Aerospike = require('aerospike')
const lists = Aerospike.lists
const key = new Aerospike.Key('test', 'demo', 'listsTest')
// INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
var config = {
  hosts: '192.168.33.10:3000',
  // Timeouts disabled, latency dependent on server location. Configure as needed.
  policies: {
    write : new Aerospike.WritePolicy({socketTimeout : 0, totalTimeout : 0}),
    operate : new Aerospike.OperatePolicy({socketTimeout : 0, totalTimeout : 0})
  }
}
Aerospike.connect(config).then(async client => {
  await client.put(key, { list: [32, 5, 85, 16, 22] })
  const ops = [
    lists.getByValueRange('list', 10, 30)
      .andReturn(lists.returnType.VALUE)
  ]
  const result = await client.operate(key, ops)
  console.log('Result:', result.bins.list) // => Result: [ 16, 22 ]
  client.close()
})
Parameters:
Name Type Description
returnType number

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

invertSelection()

Inverts the selection of items for certain list operations.

Description:
  • For getBy* and removeBy* list operations, calling the invertSelect method on the ListOperation has the effect of inverting the selection of list elements that the operation affects.

Source:
Example

Remove all tags except for yellow from the record

const Aerospike = require('aerospike')
const lists = Aerospike.lists
const key = new Aerospike.Key('test', 'demo', 'listsTest')
// INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
var config = {
  hosts: '192.168.33.10:3000',
  // Timeouts disabled, latency dependent on server location. Configure as needed.
  policies: {
    write : new Aerospike.WritePolicy({socketTimeout : 0, totalTimeout : 0}),
    operate : new Aerospike.OperatePolicy({socketTimeout : 0, totalTimeout : 0})
  }
}
Aerospike.connect(config).then(async client => {
  await client.put(key, { tags: ['blue', 'yellow', 'pink'] })
  const ops = [
    lists.removeByValue('tags', 'yellow')
      .invertSelection()
  ]
  await client.operate(key, ops)
  const record = await client.get(key)
  console.log('Result:', record.bins.tags) // => Result: [ 'yellow' ]
  client.close()
})
Throws:

if the operation is not invertible.

Type
AerospikeError

withContext(context) → {ListOperation}

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

Source:
Since:
  • v3.12.0
Examples

Fetch the 1st element of the 2nd nested list

const Aerospike = require('aerospike')
const lists = Aerospike.lists
const key = new Aerospike.Key('test', 'demo', 'listsTest')
// INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
var config = {
  hosts: '192.168.33.10:3000',
  // Timeouts disabled, latency dependent on server location. Configure as needed.
  policies: {
    write : new Aerospike.WritePolicy({socketTimeout : 0, totalTimeout : 0}),
    operate : new Aerospike.OperatePolicy({socketTimeout : 0, totalTimeout : 0})
  }
}
Aerospike.connect(config).then(async (client) => {
  await client.put(key, { list: [[32, 5, 85], [16, 22]] })
  const ops = [
    lists.get('list', 0)
      .withContext((ctx) => ctx.addListIndex(1))
  ]
  const result = await client.operate(key, ops)
  console.log('Result:', result.bins.list) // => Result: 16
  client.close()
})

Fetch the last element of the nested list stored under the 'nested' map key

const Aerospike = require('aerospike')
const lists = Aerospike.lists
const Context = Aerospike.cdt.Context
const key = new Aerospike.Key('test', 'demo', 'listsTest')
// INSERT HOSTNAME AND PORT NUMBER OF AEROSPIKE SERVER NODE HERE!
var config = {
  hosts: '192.168.33.10:3000',
  // Timeouts disabled, latency dependent on server location. Configure as needed.
  policies: {
    write : new Aerospike.WritePolicy({socketTimeout : 0, totalTimeout : 0}),
    operate : new Aerospike.OperatePolicy({socketTimeout : 0, totalTimeout : 0})
  }
}
Aerospike.connect(config).then(async (client) => {
  await client.put(key, { map: { nested: [32, 5, 85, 16, 22] } })
  const context = new Context().addMapKey('nested')
  const ops = [
    lists.get('map', -1)
      .withContext(context)
  ]
  const result = await client.operate(key, ops)
  console.log('Result:', result.bins.map) // => Result: 22
  client.close()
})
Parameters:
Name Type Description
context CdtContext | function

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

Returns:

The list operation itself.

Type
ListOperation