3

I want to filter data using dynamoDB QueryInput approach, the problem is in filtering data with an object in Array. I have a data with below structure

[
    {
        "Pk": "mimzeslami@gmail.com",
        "Sk": "social-shared-goal-2022-09-27 12:29:27",
        "Gsi1Pk": "social-shared-goal",
        "Gsi1Sk": "2022-09-27 12:29:27",
        "Username": "test@gmail.com",
        "Goals": [
            {
                "M": {
                    "Gsi2Sk": {
                        "S": "goal-end-2022-11-26 20:30:00"
                    },
                    "Gsi1Sk": {
                        "S": "goal-start-2022-09-27 12:28:47"
                    },
                    "Pk": {
                        "S": "mimzeslami@gmail.com"
                    },
                    "Gsi1Pk": {
                        "S": "mimzeslami@gmail.com"
                    },
                    "BaseCategoryId": {
                        "S": "85j85nachallll9idja"
                    },
                    "SubCategoryId": {
                        "S": "49023842874xhhiayx"
                    },
                    "Gsi2Pk": {
                        "S": "mimzeslami@gmail.com"
                    }
                }
            }
        ]
    }
]

I filtered data with Username in this way:

keyCondition := map[string]*dynamodb.Condition{
Gsi1Pk: {
ComparisonOperator: aws.String(Eq),
AttributeValueList: []*dynamodb.AttributeValue{
        {
               S: aws.String(constants.SocialSharedGoal),
        },
      },
    },  
}

    var queryFilter = map[string]*dynamodb.Condition{}
    if maybeUserName != "" {
        queryFilter["Username"] = &dynamodb.Condition{
            ComparisonOperator: aws.String("CONTAINS"),
            AttributeValueList: []*dynamodb.AttributeValue{
                {
                    S: aws.String(maybeUserName),
                },
            },
        }
    }
    
    params := &dynamodb.QueryInput{
        KeyConditions: keyCondition,
        TableName:     aws.String(myTable),
        IndexName:     aws.String(Gsi1PkGsi1SkIndex),
        Limit:         &limit,
    
        ScanIndexForward: &ascSort,
        QueryFilter:      queryFilter,
  }

Now I want to filter data by BaseCategoryId and SubCategoryId that are in a Golas array and I don't know how to do that.

I am looking for a way like this to filter data for example

if maybeBaseCategoryId != "" {
  queryFilter[""] = &dynamodb.Condition{    
            ComparisonOperator: aws.String("CONTAINS"),
        AttributeValueList: []*dynamodb.AttributeValue{
                        {S: aws.String(maybeBaseCategoryId),
                  },                            
              },                
          }     
  }
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

0 Answers0