使用bolthold进行BoltDB查询,使用3个条件。

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

Go boltDB query using bolthold 3 conditions

问题

所以我之前有一个问题,并且在问题下面有一个答案,它是有效的,但我刚刚意识到我提出的查询不按计划工作。

基本上现在它的工作方式是这样的:

(如果roleskey包含slice中的任何角色)并且(如果tenantID是空字符串)或者(如果tenantIDKey等于tenantID)

但我需要的是:

(如果roleskey包含slice中的任何角色)并且(如果tenantID是空字符串或者tenantIDKey等于tenantID)

这是我当前的查询:

query := bolthold.Where(roleskey).ContainsAny(bolthold.Slice(roles)...).And(tenantIDKey).Eq("").Or(bolthold.Where(tenantIDKey).Eq(tenantID))

有人知道如何解决这个问题吗?

英文:

So I had this question before and I had an answer below the question which worked, but I just realized that the query I came out with does not work as planned.

Basically now if it works like this

(if the roleskey contains any of the roles in slice) and (if the tenantID is an empty string) or (if tenantIDKey is equal to tenantID)

But what I need is

(if the roleskey contains any of the roles in slice) AND (if the tenantID is an empty string OR if tenantIDKey is equal to tenantID)

Here is my current query:

query := bolthold.Where(roleskey).ContainsAny(bolthold.Slice(roles)...).And(tenantIDKey).Eq("").Or(bolthold.Where(tenantIDKey).Eq(tenantID))

Does anyone know how to solve this?

答案1

得分: 2

尝试:

query := bolthold.
         Where(tenantIDKey).Eq("").
         Or(
           bolthold.
           Where(tenantIDKey).
           Eq(tenantID)
         ).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)

或者

query := bolthold.
         Where(tenantIDKey).ContainsAny("", tenantID).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)

或者

query := bolthold.
         Where(tenantIDKey).In("", tenantID).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)
英文:

Try:

query := bolthold.
         Where(tenantIDKey).Eq("").
         Or(
           bolthold.
           Where(tenantIDKey).
           Eq(tenantID)
         ).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)

Or

query := bolthold.
         Where(tenantIDKey).ContainsAny("", tenantID).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)

Or

query := bolthold.
         Where(tenantIDKey).In("", tenantID).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)

huangapple
  • 本文由 发表于 2022年5月13日 15:59:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/72226242.html
匿名

发表评论

匿名网友

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

确定