kumar gaurav
kumar gaurav

Reputation: 43

Aerospike Delete Record in list if element of bin known

I have a set that have three bins ( PK, cat_id and data ). List index is applied on cat_id . I am able to select record by query:

SELECT * FROM test.myset IN LIST where cat_id = '1'

It is working fine for me. Now I need to delete this record by same condition. But as I have read that PK is necessary to delete any record . In my case i have cat_id to delete this record.

Help me to delete this record by using bin element not PK. I am using PHP for it. AQL also work for me.

Upvotes: 2

Views: 840

Answers (1)

Ronen Botzer
Ronen Botzer

Reputation: 7117

You would use an asynchronous background query that applies a tiny (record UDF) Lua function to every record matched by that query's predicate. In the PHP client, you'd use the queryApply() method for this.

This Lua function simply says 'die':

function del_rec(rec)
    aerospike:remove(rec)
end

I used the Lua UDF API reference for this 'complex' function, mainly the aerospike object reference. If you wanted to do more logic, like check on other bins, you'd be using the record methods. There's more info on UDFs and developing Record UDFs at the Aerospike website.

Once you register UDF module (the file containing this function) with the cluster, you can call it from your PHP code:

$where = Aerospike::predicateContains("cat_id", Aerospike::INDEX_TYPE_LIST, 1);
$status = $client->queryApply("test", "mytest", $where, "my_udfs", "del_rec", [], $job_id);
if ($status === Aerospike::OK) {
    var_dump("The background job ID is $job_id");
} else {
    echo "An error occured while initiating the background query [{$client->errorno()}] ".$client->error();
}

Upvotes: 2

Related Questions