使用bolthold进行BoltDB查询

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

Go boltDB query using bolthold

问题

我正在尝试使用Go创建一个用于BoltDB的查询。

我需要一个查询来检查角色和租户ID。基本上,该项必须始终包含角色,如果该项具有租户ID,则必须匹配,但是如果该项没有租户ID(如果为nil),也必须返回它。

因此,我提出了下面的查询,它检查角色和租户ID,但不检查租户ID是否为nil。有人可以帮助我吗?我需要在这个查询中添加什么来考虑到nil的租户ID?

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

英文:

I am trying to create a query for boltDB using Go.

I have to have a query that checks for the roles and the tenantID. Basically the item has to always contain the role, also if the item has a tenantID it has to match however if the item does not have a tenantID (if it's nil) it also has to return it.

So I came up with the query below that checks for the role and the tenantID, but doesn't check if the tenantID is nil. Could anyone help me with this? What would I need to add to this query would take into account the tenantID of nil?

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

答案1

得分: 1

你可以使用Or条件来检查是否等于tenantIDKeyIsNil

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

你也可以使用In代替ContainsAny

query := bolthold.
         Where(roleskey).
         In(bolthold.Slice(roles)...).
         And(tenantIDKey).IsNil().
         Or(
           bolthold.
           Where(tenantIDKey).
           Eq(tenantID)
         )
英文:

You can use Or condition to check if it is equal to tenantIDKey or IsNil

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

You can also you In in place of ContainsAny

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

huangapple
  • 本文由 发表于 2022年4月29日 20:35:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/72058075.html
匿名

发表评论

匿名网友

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

确定