douliang1369 2018-10-30 21:32
浏览 93
已采纳

适用于Go的AWS开发工具包-DynamoDb-向FilterExpression添加多个条件

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.

This won't work

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)

}

This is a working example with a single filter

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"),
}

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?

  • 写回答

1条回答 默认 最新

  • dsf1222 2018-10-30 22:04
    关注

    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)
    }
    

    Documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?