Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete entry in couchbase bucket using key in the form of regex

Tags:

couchbase

I have a requirement wherein I have to delete an entry from the couchbase bucket. I use the delete method of the CouchbaseCient from my java application to which I pass the key. But in one particular case I dont have the entire key name but a part of it. So I thought that there would be a method that takes a matcher but I could not find one. Following is the actual key that is stored in the bucket

  123_xyz_havefun

and the part of the key that I have is xyz. I am not sure whether this can be done. Can anyone help.

like image 932
user1305398 Avatar asked Jan 11 '23 07:01

user1305398


2 Answers

The DELETE operation of the Couchbase doesn't support neither wildcards, nor regular expressions. So you have to get the list of keys somehow and pass it to the function. For example, you might use Couchbase Views or maintain your own list of keys via APPEND command. Like create the key xyz and append to its value all the matching keys during application lifetime with flushing this key after real delete request

like image 57
avsej Avatar answered Jan 13 '23 19:01

avsej


Well, I think you can achieve delete using wildcard or regex like expression.

Above answers basically says, - Query the data from the Couchbase - Iterate over resultset - and fire delete for each key of your interest.

However, I believe: Delete on server should be delete on server, rather than requiring three steps as above.

In this regards, I think old fashioned RDBMS were better all you need to do is fire SQL query like 'DELETE * from database where something like "match%"'.

Fortunately, there is something similar to SQL is available in CouchBase called N1QL (pronounced nickle). I am not aware about JavaScript (and other language syntax) but this is how I did it in python.

Query to be used: DELETE from b where META(b).id LIKE "%"

layer_name_prefix = cb_layer_key + "|" + "%"
query = ""
try:
    query = N1QLQuery('DELETE from `test-feature` b where META(b).id LIKE $1', layer_name_prefix)
    cb.n1ql_query(query).execute()
except CouchbaseError, e:
    logger.exception(e)

To achieve the same thing: alternate query could be as below if you are storing 'type' and/or other meta data like 'parent_id'.

DELETE from where type='Feature' and parent_id=8;

But I prefer to use first version of the query as it operates on key, and I believe Couchbase must have some internal indexes to operate/query faster on key (and other metadata).

like image 39
Jadav Bheda Avatar answered Jan 13 '23 21:01

Jadav Bheda