英文:
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
条件来检查是否等于tenantIDKey
或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()
)
你也可以使用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)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论