英文:
RethinkDB - Filter using Match() by value in same dataset and table
问题
所以,由于显然我太笨了,无法自己解决这个问题,我会在这里向大家请教。
基本上,我有一个数据结构,看起来像这样:
....,
{
"id": 12345
....
"policy_subjects": [
{
"compiled": "^(user|max|anonymous)$",
"template": "<user|max|anonymous>"
},
{
"compiled": "^max$",
"template": "max"
}
]
....
}
compiled 是一个“编译过的”正则表达式
template 是没有正则表达式修饰符的相同正则表达式
我想要做的是在 RethinkDB 中使用“compiled”值进行简单的查询,并将其与字符串(比如“max”)进行匹配。
基本上,我想要做的是:
r.table("regex_policies").filter(function(policy_row) {
return "max".match("(?i)"+policy_row("policy_subjects")("compiled"))
}
这是我想要做的(不区分大小写的搜索)。
数据库中当然有很多 policy_subjects,所以在这个例子中,结果应该是与“max”匹配的整个数据集(1个结果)。因为在这种情况下,“max”出现了两次,并且它同时匹配了两个正则表达式(一次匹配就足够了)。
在这个例子中,“foobar”将不会产生任何结果,因为任何一个编译过的正则表达式都不匹配“foobar”。
有人知道如何执行这个相对简单的查询吗?
英文:
So, since I'm too dumb obviously to figure this out myself, I'll ask you better folks here on SO instead.
Basically i have a datastructure that looks like the following:
....,
{
"id": 12345
....
"policy_subjects": [
{
"compiled": "^(user|max|anonymous)$",
"template": "<user|max|anonymous>"
},
{
"compiled": "^max$",
"template": "max"
}
]
....
}
compiled is a "compiled" regex
template is the same regex without regex-modifiers
What I want is to do a simple query in RethinkDB using the "compiled" value and matching that against a string, say "max".
Basically
r.table("regex_policies").filter(function(policy_row) {
return "max".match("(?i)"+policy_row("policy_subjects")("compiled"))
}
Is what i want to do (+case-insensitive search)
There are of course lots of policy_subjects in the database so in this example the result should be the whole dataset (1 result) that matches "max". Since "max" exists twice in this case and it matches both regexes (once would have been enough).
"foobar" would likewise in this example yield 0 results, since any of the compiled regexes does not match "foobar".
Does anyone know how to do this relatively simple query?
答案1
得分: 1
你肯定想在这里使用r.expr
,我成功运行了这个例子:
r.expr([{
"id": 12345,
"policy_subjects": [
{
"compiled": "^(user|max|anonymous)$",
"template": "<user|max|anonymous>"
},
{
"compiled": "^max$",
"template": "max"
}
]
}]).merge(function(policy_row) {
return {
"policy_subjects": policy_row("policy_subjects").filter(function(item){
return r.expr("max").match(r.expr("(?i)").add(item("compiled"))).ne(null);
})
}
})
将max
更改为不匹配的其他内容,将返回没有policy_subjects
元素的文档。
例如,将max
更改为wat
(我有史以来最喜欢的测试字符串),如下所示:
.merge(function(policy_row) {
return {
"policy_subjects": policy_row("policy_subjects").filter(function(item){
return r.expr("wat").match(r.expr("(?i)").add(item("compiled"))).ne(null);
})
}
})
结果如下:
[
{
"id": 12345,
"policy_subjects": []
}
]
我认为你想要缩减到你想要的一个policy_subject
文档的逻辑可能有点主观,所以我不确定正确答案是什么,但你可以使用.reduce(...)
来返回最右边的值。
英文:
You definitely want to use r.expr
here and I got this example to work:
r.expr([{
"id": 12345,
"policy_subjects": [
{
compiled: "^(user|max|anonymous)$",
template: "<user|max|anonymous>"
},
{
compiled: "^max$",
template: "max"
}
]
}]).merge(function(policy_row) {
return {
"policy_subjects": policy_row("policy_subjects").filter(function(item){
return r.expr("max").match(r.expr("(?i)").add(item("compiled"))).ne(null);
})
}
})
Changing max
to something else that does not match, returns the document with no elements inside policy_subjects
.
For example, changing max
=> to wat
(my favorite test string of all time) looks like this:
.merge(function(policy_row) {
return {
"policy_subjects": policy_row("policy_subjects").filter(function(item){
return r.expr("wat").match(r.expr("(?i)").add(item("compiled"))).ne(null);
})
}
})
And results in this:
[
{
"id": 12345 ,
"policy_subjects": [ ]
}
]
I think your logic for reducing to the one policy_subject
document you want might be a little subjective to your use case so I'm not sure what the right answer is but you can use .reduce(...)
to just return the right-most value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论