亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 DynamoDB Go SDK 中編寫查詢,其中分區鍵的條件相等,但一組分區值的排序鍵具有

如何在 DynamoDB Go SDK 中編寫查詢,其中分區鍵的條件相等,但一組分區值的排序鍵具有

Go
泛舟湖上清波郎朗 2022-08-15 15:39:09
我在 DynamoDB 中有一個帶有復合鍵的表user_id(分區鍵)時間戳(排序鍵)我想檢索一組項目,我有一組user_ids我想檢索,但我也需要時間戳是每個user_id的最大值。例如:user_id時間戳阿特11614910613值111614902527值 221614910683值 221614881311值 231614902527值 2我想檢索user_id 1 和 2 的行,以及每個用戶的最大時間戳。在這種情況下,我的結果集應該是:user_id時間戳阿特11614910613值121614910683值 2我現在可以逐項執行此操作,執行以下 Key 表達式:cond := expression.KeyAnd(    expression.Key("user_id").Equal(expression.Value(userId)),    expression.Key("timestamp").LessThanEqual(expression.Value(int32(time.Now().Unix()))),    )expr, err:= expression.NewBuilder().WithKeyCondition(cond).Build()if err!=nil {    panic(err)}input := &dynamodb.QueryInput{    ExpressionAttributeNames: expr.Names(),    ExpressionAttributeValues: expr.Values(),    KeyConditionExpression: expr.KeyCondition(),    TableName: aws.String("td.notes"),    Limit: aws.Int64(1),    ScanIndexForward: aws.Bool(false),}我的問題是,我不知道如何傳遞user_id鍵的一組值,而不是單個值。我看了一下 BatchGetItem,我知道我可以做這樣的事情:mapOfAttrKeys := []map[string]*dynamodb.AttributeValue{}for _, place := range userIDs {    mapOfAttrKeys = append(mapOfAttrKeys, map[string]*dynamodb.AttributeValue{        "user_id": &dynamodb.AttributeValue{            S: aws.String(place),        },        "timestamp": &dynamodb.AttributeValue{            N: aws.String(timestamp),        },    })}input := &dynamodb.BatchGetItemInput{    RequestItems: map[string]*dynamodb.KeysAndAttributes{        tableName: &dynamodb.KeysAndAttributes{            Keys: mapOfAttrKeys,        },    },}但是我不能在時間戳中放置“小于”條件。
查看完整描述

1 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

GetItem 和 BatchGetItem 需要確切的主鍵。

您需要為要返回的每個用戶發出單獨的 Query()。

返回 max 的關鍵是你所發現的

Limit: aws.Int64(1),
ScanIndexForward: aws.Bool(false),

你真的不應該需要這個

expression.Key("timestamp").LessThanEqual(expression.Value(int32(time.Now().Unix())))



查看完整回答
反對 回復 2022-08-15
  • 1 回答
  • 0 關注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號