适用于Go的AWS开发工具包-DynamoDb-向FilterExpression添加多个条件 [英] AWS SDK for Go - DynamoDb - Add multiple conditions to FilterExpression
问题描述
我正在尝试使用表达式生成器按多种条件过滤DynamoDb扫描。根据此博客帖子,尝试在构建器中添加其他条件将覆盖以前的条件。必须有某种方法可以添加其他条件,但是我一直无法找到一种方法来添加它。
I am attempting to filter a DynamoDb scan by multiple conditions using the expression builder. According to this blog post, attempting to add another condition in the builder will overwrite the previous condition. There must be some way to add another condition, but I have not been able to find a way to do it.
cond1 := expression.Name("foo").Equal(expression.Value(5))
cond2 := expression.Name("bar").Equal(expression.Value(6))
expr, err := expression.NewBuilder().
WithCondition(cond1).
WithCondition(cond2).
Build()
if err != nil {
fmt.Println(err)
}
filt := expression.Name("Artist").Equal(expression.Value("No One You Know"))
proj := expression.NamesList(
expression.Name("SongTitle"),
expression.Name("AlbumTitle"),
)
expr, err := expression.NewBuilder().
WithFilter(filt).
WithProjection(proj).
Build()
if err != nil {
fmt.Println(err)
}
input := &dynamodb.ScanInput{
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
FilterExpression: expr.Filter(),
ProjectionExpression: expr.Projection(),
TableName: aws.String("Music"),
}
我已经能够在不使用表达式buidler的情况下完成此操作,但是我更喜欢使用表达式构建器。我如何才能向该过滤器添加另一个条件?
I have been able to accomplish this without using the expression buidler, but I would prefer to use the expression builder. How could I add another condition to that filter?
推荐答案
您可以尝试使用 And
,或
和 Not
的方法来自 ConditionBuilder
结构。示例:
You can try adding multiple conditions with And
, Or
and Not
methods from the ConditionBuilder
struct. Example:
cond1 := expression.Name("foo").Equal(expression.Value(5))
cond2 := expression.Name("bar").Equal(expression.Value(6))
expr, err := expression.NewBuilder().
WithCondition(cond1.And(cond2)).
Build()
if err != nil {
fmt.Println(err)
}
文档。
这篇关于适用于Go的AWS开发工具包-DynamoDb-向FilterExpression添加多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!