Aerospike database - How do I append to a list that is a value of a map

Viewed 155

I have a very simple map with a single key and the value is a list.

client.put(key, 
            {bin_name:
                {
                    1523474230000: ['fav', 'fav1']
                }
            }
)

I just want to append one more value to this list using the operate command so as to preserve atomicity. I can't figure out how to do this.

map_operations doesn't seem to have the required APIs list_operations.append_list is not the correct API.

This must be one of the most common operations performed in a key value store. So I am quite baffled at my own inability to find answers to these simple ops.

Can I club map_operations and list_operations in the operate command ? If so, how. Will appreciate any help.

Disclaimer - I work at Aerospike and I am new. I am putting this out on SO so that it benefits other community members as well.

1 Answers

Turns out this is a super simple thing to do in Aerospike. All you need is this entity called a Context. More details on context here

So applying that context to the above example

client.put(key, 
            {bin_name:
                {
                    1523474230000: ['fav', 'fav1']
                }
            }
)
ctx = [
    cdt_ctx.cdt_ctx_map_key(1523474230000)
]
ops = [
    # We want to append to a list, so we use a list_operation.  The top level being a map is dealt with using ctx.
    list_operations.list_append(bin_name, 'val2', ctx=ctx),
]
client.operate(key, ops)
_, _, result = client.get(key)
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(result)

Result:

{'complex_map_bin': {1523474230000: ['fav', 'fav1', 'val2']}}
Related