Why Contains in .Filter(func()) is not working in gorethink and parts of query get ignored?

huangapple go评论79阅读模式
英文:

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作为过滤条件。

英文:

Since the Filter method returns a new Term, which will contain the expression of the previous term, you need to re-assing the result back to q.

if some_condition {
   q = q.Filter(some_filter)
}

if some_other_condition {
   q = q.Filter(some_other_filter)
}

huangapple
  • 本文由 发表于 2017年5月16日 16:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/43995683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定