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.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog