info2000
info2000

Reputation: 183

How run a multiple predicate mapkeys filter in aerospike nodejs client?

In Aerospike, I need run a query based on mapkeys values, filtering by records having 2 values

My predicate for filter by one value looks like this, and it work

predexp.stringValue('t3'),
predexp.stringVar('item'), 
predexp.stringEqual(),      
predexp.mapBin('category'),
predexp.mapKeyIterateAnd('item')  

I tried duplicating the code, using the "and()" method, etc but all tries return error code 4

some help, please?

Upvotes: 0

Views: 321

Answers (1)

Jan Hecking
Jan Hecking

Reputation: 949

I'm not entirely sure I understand your question. I think you want to find all records that have a map bin categories that has at least two keys t3 and some other specific value. Correct?

Here is how you would do that:

const as = require('aerospike')

as.connect().then(async client => {
  await client.put(new as.Key('test', 'qq', 1), { categories: { red: 1, blue: 2, green: 3 } })
  await client.put(new as.Key('test', 'qq', 2), { categories: { blue: 2, green: 3, yellow: 4 } })

  const query = client.query('test', 'qq')
  query.predexp = [

    // Find any record that has a category blue.
    as.predexp.stringValue('blue'),
    as.predexp.stringVar('cat'),
    as.predexp.stringEqual(),
    as.predexp.mapBin('categories'),
    as.predexp.mapKeyIterateOr('cat'),

    // Find any record that has a category yellow.
    as.predexp.stringValue('yellow'),
    as.predexp.stringVar('cat'),
    as.predexp.stringEqual(),
    as.predexp.mapBin('categories'),
    as.predexp.mapKeyIterateOr('cat'),

    // Now combine the two expression using and() to find records with categories blue AND yellow.
    // Use or(2) if you want to find records that have categories blue OR yellow.
    as.predexp.and(2)
  ]

  // prints [ { green: 3, yellow: 4, blue: 2 } ]
  await query.results()
       .then(records => records.map(r => r.bins.categories))
       .then(console.log)

  client.close()
})

Note that I'm using mapKeyIterateOr instead of mapKeyIterateAnd. That's because of each of the two subexpressions, I want any key of the categories map to be blue/yellow. If you use mapKeyIterateAnd that means you want every key of the map to meet your condition.

Upvotes: 1

Related Questions