我在 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())))
- 1 回答
- 0 關注
- 110 瀏覽
添加回答
舉報
0/150
提交
取消