在Aerospike中,我需要根据mapkeys值运行查询,通过具有2个值的记录进行过滤
过滤一个值的谓词看起来像这样,它可以工作
predexp.stringValue('t3'),
predexp.stringVar('item'),
predexp.stringEqual(),
predexp.mapBin('category'),
predexp.mapKeyIterateAnd('item')
我尝试使用“and()”方法等复制代码,但所有尝试都返回错误代码4
请帮忙吗?
我不完全确定我理解你的问题。我想你想要找到所有记录都有一个地图bin categories
,它至少有两个密钥t3
和一些其他特定值。正确?
以下是您将如何做到这一点:
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()
})
请注意,我使用mapKeyIterateOr
而不是mapKeyIterateAnd
。这是因为两个子表达式中的每一个,我想要categories
地图的任何关键字是blue
/ yellow
。如果您使用mapKeyIterateAnd
意味着您希望地图的每个键都符合您的条件。