Methods
andReturn(returnType)
Set the return type for certain map operations.
- Description:
The return type only affects
getBy*
andremoveBy*
map operations.
- 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')
// 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: { 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()
})
Parameters:
Name | Type | Description |
---|---|---|
returnType |
number | The |
withContext(context) → {MapOperation}
By setting the context, the map operation will be executed on a nested map, instead of the bin value itself.
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')
// 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, { 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')
// 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, { 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()
})
Parameters:
Name | Type | Description |
---|---|---|
context |
CdtContext | function | Either a CdtContext object, or a function which accepts a CdtContext object. |
Returns:
The map operation itself.
- Type
- MapOperation