在RethinkDB上使用OR条件的两个过滤器。

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

Two filters with an OR-condition on RethinkDB

问题

我有一个结构体:

  1. type Talk struct {
  2. Id string `gorethink:"id,omitempty"`
  3. MatchId string
  4. UserIdX string
  5. UserIdY string
  6. UserNameX string
  7. UserNameY string
  8. CreatedAt time.Time
  9. }

我的当前Talk结构体如下所示:

  1. {
  2. "CreatedAt": "Wed Sep 14 2016 21:36:26 GMT+02:00",
  3. "MatchId": "172d51fa-438b-49a5-bbe5-422377f09336",
  4. "UserIdX": "acc4e0b6-d33b-4755-9c0a-ae5309c2ba75",
  5. "UserIdY": "03f76d8b-ed6a-4c0f-9cde-27b17c9e7cdb",
  6. "UserNameX": "Barbara",
  7. "UserNameY": "Louis",
  8. "id": "ead3f1b0-b242-4c6d-8027-a59572b58649"
  9. }

如何使用单个查询检索对话,其中:

(UserIdX == talk.UserIdX AND UserIdY == talk.UserIdY) OR (UserIdX == talk.UserIdY AND UserIdY == talk.UserIdX)

我目前是这样做的:

  1. func (talk *Talk) GetTalkByUsersId() bool {
  2. talk1 := new(Talk)
  3. talk2 := new(Talk)
  4. curs, _ := r.Table("Talks").
  5. Filter(r.Row.Field("UserIdX").Eq(talk.UserIdX)).
  6. Filter(r.Row.Field("UserIdY").Eq(talk.UserIdY)).
  7. Run(api.Sess)
  8. curs2, _ := r.Table("Talks").
  9. Filter(r.Row.Field("UserIdX").Eq(talk.UserIdY)).
  10. Filter(r.Row.Field("UserIdY").Eq(talk.UserIdX)).
  11. Run(api.Sess)
  12. curs.One(&talk1)
  13. curs2.One(&talk2)
  14. if talk1.Id == "" && talk2.Id == "" {
  15. return false
  16. }
  17. if talk1.Id != "" {
  18. talk.copyTalk(talk1)
  19. } else {
  20. talk.copyTalk(talk2)
  21. }
  22. return true
  23. }

如何以更简单的方式实现这个功能?

英文:

I've a struct :

  1. type Talk struct {
  2. Id string `gorethink:"id,omitempty"`
  3. MatchId string
  4. UserIdX string
  5. UserIdY string
  6. UserNameX string
  7. UserNameY string
  8. CreatedAt time.Time
  9. }

My current Talk struct looks like this:

  1. {
  2. "CreatedAt": Wed Sep 14 2016 21:36:26 GMT+02:00 ,
  3. "MatchId": "172d51fa-438b-49a5-bbe5-422377f09336" ,
  4. "UserIdX": "acc4e0b6-d33b-4755-9c0a-ae5309c2ba75" ,
  5. "UserIdY": "03f76d8b-ed6a-4c0f-9cde-27b17c9e7cdb" ,
  6. "UserNameX": "Barbara" ,
  7. "UserNameY": "Louis" ,
  8. "id": "ead3f1b0-b242-4c6d-8027-a59572b58649"
  9. }

How can I retrieve a talk, with a single query, where:

> (UserIdX == talk.UserIdX AND UserIdY == talk.UserIdY) OR (UserIdX ==
> talk.UserIdY AND UserIdY == talk.UserIdX)

I actually do it like the following:

  1. func (talk *Talk) GetTalkByUsersId() bool {
  2. talk1 := new(Talk)
  3. talk2 := new(Talk)
  4. curs, _ := r.Table("Talks").
  5. Filter(r.Row.Field("UserIdX").Eq(talk.UserIdX)).
  6. Filter(r.Row.Field("UserIdY").Eq(talk.UserIdY)).
  7. Run(api.Sess)
  8. curs2, _ := r.Table("Talks").
  9. Filter(r.Row.Field("UserIdX").Eq(talk.UserIdY)).
  10. Filter(r.Row.Field("UserIdY").Eq(talk.UserIdX)).
  11. Run(api.Sess)
  12. curs.One(&talk1)
  13. curs2.One(&talk2)
  14. if talk1.Id == "" && talk2.Id == "" {
  15. return false
  16. }
  17. if talk1.Id != "" {
  18. talk.copyTalk(talk1)
  19. } else {
  20. talk.copyTalk(talk2)
  21. }
  22. return true
  23. }

How can I get this to work in a much simpler way?

答案1

得分: 2

我将在这里召唤 @daniel-cannon,但我认为这就是你要找的,并且将大大简化这个查询并将其减少为一个查询。但是有两个提示:

  1. r.Table("talks").Filter(func(talk r.Term) r.Term {
  2. return r.Or(
  3. r.And(talk.Field("UserIdX").Eq(UserIdX), talk.Field("UserIdX").Eq(UserIdY)),
  4. r.And(talk.Field("UserIdY").Eq(UserIdX), talk.Field("UserIdX").Eq(UserIdY)),
  5. )
  6. }).Run(api.Sess)

希望这可以帮到你!

英文:

I'm going to summon @daniel-cannon here but I think this is what you're looking for and will vastly simplify this query and reduce it to just a single query. But just two tips:

<!-- language: lang-golang -->

  1. r.Table(&quot;talks&quot;).Filter(func(talk r.Term) r.Term {
  2. return r.Or(
  3. r.And(talk.Field(&quot;UserIdX&quot;).Eq(UserIdX), talk.Field(&quot;UserIdX&quot;).Eq(UserIdY)),
  4. r.And(talk.Field(&quot;UserIdY&quot;).Eq(UserIdX), talk.Field(&quot;UserIdX&quot;).Eq(UserIdY)),
  5. )
  6. }).Run(api.Sess)

I hope this helps!

huangapple
  • 本文由 发表于 2016年9月15日 04:04:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/39498797.html
匿名

发表评论

匿名网友

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

确定