适用于 Go 的 AWS 开发工具包 - DynamoDb - 向 FilterExpression 添加多个条件 [英] AWS SDK for Go - DynamoDb - Add multiple conditions to FilterExpression

查看:18
本文介绍了适用于 Go 的 AWS 开发工具包 - DynamoDb - 向 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"),
}

我已经能够在不使用表达式构建器的情况下完成此操作,但我更喜欢使用表达式构建器.如何向该过滤器添加另一个条件?

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?

推荐答案

您可以尝试使用 AndOrNot 添加多个条件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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆