I am looking for an example of using the cmpRegex with javascript

Hi I am trying to implement a cmpRegex search with javascript. I have looked at the documentation and into the code and could not really figure out the way to do it.

I have managed to implement it in python here is the code:

import aerospike
import sys
from aerospike import exception as ex
from aerospike import predicates
from aerospike_helpers import expressions as exp

config = {'hosts': [('127.0.0.1', 3000)]}
client = aerospike.client(config).connect()


client.index_string_create('test', 'test1', 'name', 'test1_name_idx')
for i in range(1000):
    key = ('test', 'test1', str(i))
    rec = {
        'name': 'test' + str(i),
        'tags': ['test' + str(i), 'test' + str(i+1)]
    }
    client.put(key, rec)
def showRes(record):
    print(record[2].get("name"))

query = client.query('test', 'test1')
q_regex = exp.CmpRegex(aerospike.REGEX_ICASE, "^test55.*.", exp.StrBin("name")).compile()
scan_policy={"expressions": q_regex}

records = query.results(scan_policy)
for r in records:
    showRes(r)



1 Like

Hey! I was digging into this recently too, and yeah ..examples with cmpRegex in JavaScript are pretty hard to come by.

What worked for me was using it inside a filter expression like this:

const exp = aerospike.exp;
const regexFilter = exp.regexCompare('^abc', exp.bin('myBin'));

Then you can pass it as a filter expression when doing a query or scan:

const policy = new aerospike.QueryPolicy({
  filterExpression: regexFilter
});

Just make sure your server version supports regex expressions — I think it needs to be 5.6 or higher.

Hope that helps! Would be great to see more complete examples in the docs, honestly.

The regex compare was supported in Aerospike 5.2.