Загрузка...

DynamoDB filter on a nested field value using Golang

Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: https://www.instagram.com/ky.emrah

Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!DynamoDB filter on a nested field value using Golang

Table name: MyTable
Attribute name: data
Keyword to filter: rally
MyTable
data
rally
Given an example value, how do I filter items where the attributes.label array contains a rally?
attributes.label
rally
{
"type": {
"S": "play"
},
"attributes": {
"M": {
"term": {
"S": "end_term"
},
"label": {
"L": [
{
"S": "all_players"
},
{
"S": "rally"
},
{
"S": "race"
}
]
},
"currency": {
"S": "USD"
}
}
},
"id": {
"S": "a752d2"
}
}

{
"type": {
"S": "play"
},
"attributes": {
"M": {
"term": {
"S": "end_term"
},
"label": {
"L": [
{
"S": "all_players"
},
{
"S": "rally"
},
{
"S": "race"
}
]
},
"currency": {
"S": "USD"
}
}
},
"id": {
"S": "a752d2"
}
}

I used example below but it always returns 0 items.
0
input := &dynamodb.ScanInput{
TableName: aws.String("MyTable"),
FilterExpression: aws.String("contains(#path, :keyword)"),
ExpressionAttributeNames: map[string]string{
"#path": "attributes.label",
},
ExpressionAttributeValues: map[string]types.AttributeValue{
":keyword": &types.AttributeValueMemberS{Value: "rally"},
},
}

result, err := c.client.Scan(context.TODO(), input)
if err != nil {
log.Fatalf("failed to scan items: %v", err)
}

fmt.Println(" Result", len(result.Items))

for i, item := range result.Items {
var data map[string]interface{}
err := json.Unmarshal([]byte(item["data"].(*types.AttributeValueMemberS).Value), &data)
if err != nil {
log.Printf("failed to unmarshal item: %v", item)
continue
}
fmt.Printf(" Item (%d): %+v", i, data)
}

input := &dynamodb.ScanInput{
TableName: aws.String("MyTable"),
FilterExpression: aws.String("contains(#path, :keyword)"),
ExpressionAttributeNames: map[string]string{
"#path": "attributes.label",
},
ExpressionAttributeValues: map[string]types.AttributeValue{
":keyword": &types.AttributeValueMemberS{Value: "rally"},
},
}

result, err := c.client.Scan(context.TODO(), input)
if err != nil {
log.Fatalf("failed to scan items: %v", err)
}

fmt.Println(" Result", len(result.Items))

for i, item := range result.Items {
var data map[string]interface{}
err := json.Unmarshal([]byte(item["data"].(*types.AttributeValueMemberS).Value), &data)
if err != nil {
log.Printf("failed to unmarshal item: %v", item)
continue
}
fmt.Printf(" Item (%d): %+v", i, data)
}
Tags: amazon-web-services,go,amazon-dynamodbSource of the question:
https://stackoverflow.com/questions/79007384

Question and source license information:
https://meta.stackexchange.com/help/licensing
https://stackoverflow.com/

Видео DynamoDB filter on a nested field value using Golang канала Emrah KAYA
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять