英文:
Why Contains in .Filter(func()) is not working in gorethink and parts of query get ignored?
问题
我正在尝试做这个:
rethink.Table(table).GetAllByIndex(index, value).Filter(func(row rethink.Term) interface {}{
return rethink.Expr([]string{}).Contains(row.Field("type"))
})
我不确定,但是好像忽略了rethink.Expr。
这是第一个问题。
第二个问题是这样的。如果我有这样的查询语句:
query := rethink.Table(table).GetAllByIndex(index, value)
然后尝试执行以下操作:
if some_condition {
q.Filter(some_filter)
}
if some_other_condition {
q.Filter(some_other_filter)
}
当我打印出q.String()时,我只得到了第一部分,其他部分被忽略了rethink.Table(table).GetAllByIndex(index, value)。
英文:
I am trying to do this:
r.table(table).filter(
function (doc) {
return r.expr(array)
.contains(doc("name"));
}
)
written in golang that is
rethink.Table(table).GetAllByIndex(index, value).Filter(func(row rethink.Term) interface {}{
return rethink.Expr([]string{}).Contains(row.Field("type"))
})
I am not sure but it is like rethink.Expr is ignored.
That is first problem.
Second problem is next. If I have query written like this:
query := rethink.Table(table).GetAllByIndex(index, value)
and then try to do next:
if some_condition {
q.Filter(some_filter)
}
if some_other_condition {
q.Filter(some_other_filter)
}
when i print out q.String() i got only that first part and everything else is ignored rethink.Table(table).GetAllByIndex(index, value)
答案1
得分: 0
由于Filter方法返回一个新的Term,其中包含了前一个Term的表达式,所以你需要将结果重新赋值给q。
if some_condition {
q = q.Filter(some_filter)
}
if some_other_condition {
q = q.Filter(some_other_filter)
}
这段代码的作用是,如果some_condition满足,就对q进行过滤操作,使用some_filter作为过滤条件;如果some_other_condition满足,就再次对q进行过滤操作,使用some_other_filter作为过滤条件。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论