Get operations
MapGetByKeyOp: get value by key
Retrieves a value from a map by its key. This is one of the most commonly used map operations.
Function signature:
MapGetByKeyOp(binName string, key interface{}, returnType mapReturnType, ctx ...*CDTContext) *OperationParameters:
binName: Name of the bin containing the map.key: The key to look up (string, int64, or interface{} supported type).returnType: What to return (see Return Types).ctx: Optional CDT context for nested maps.
Example: get value (most common)
// Get a value by keyrecord, err := client.Operate(nil, key, as.MapGetByKeyOp("profile", "email", as.MapReturnType.VALUE),)
if err != nil { log.Fatal(err)}
email := record.Bins["profile"].(string)fmt.Println("Email:", email) // Output: Email: john@example.comExample: e-commerce product catalog
// Store product informationproduct := map[string]any{ "name": "Laptop Pro 15", "price": 1299.99, "stock": 42, "category": "Electronics", "description": "High-performance laptop",}
key, _ := as.NewKey("store", "products", "prod123")_, err := client.Operate(nil, key, as.MapPutItemsOp(as.DefaultMapPolicy(), "product", product),)
// Later, retrieve product pricerecord, err := client.Operate(nil, key, as.MapGetByKeyOp("product", "price", as.MapReturnType.VALUE),)
price := record.Bins["product"].(float64)fmt.Printf("Price: $%.2f\n", price)Example: check if key exists
// Check if a key existsrecord, err := client.Operate(nil, key, as.MapGetByKeyOp("profile", "email", as.MapReturnType.EXISTS),)
exists := record.Bins["profile"].(bool)
if exists { fmt.Println("Email is set")} else { fmt.Println("Email not found")}Example: get key-value pair
// Get both key and valuerecord, err := client.Operate(nil, key, as.MapGetByKeyOp("profile", "name", as.MapReturnType.KEY_VALUE),)
pairs := record.Bins["profile"].([]as.MapPair)fmt.Printf("Key: %v, Value: %v\n", pairs[0].Key, pairs[0].Value)Example: get multiple values in one operation
// Get multiple values at oncerecord, err := client.Operate(nil, key, as.MapGetByKeyOp("profile", "name", as.MapReturnType.VALUE), as.MapGetByKeyOp("profile", "age", as.MapReturnType.VALUE), as.MapGetByKeyOp("profile", "email", as.MapReturnType.VALUE),)
results := record.Bins["profile"].(as.OpResults)name := results[0].(string)age := results[1].(int)email := results[2].(string)
fmt.Printf("Name: %s, Age: %d, Email: %s\n", name, age, email)Example: working with integer keys
// Map with integer keys (e.g., user IDs)scores := map[any]any{ 1001: 1500, 1002: 2300, 1003: 1800,}
key, _ := as.NewKey("game", "scores", "leaderboard")_, err := client.Operate(nil, key, as.MapPutItemsOp(as.DefaultMapPolicy(), "scores", scores),)
// Get score for user 1002record, err := client.Operate(nil, key, as.MapGetByKeyOp("scores", 1002, as.MapReturnType.VALUE),)
score := record.Bins["scores"].(int)fmt.Printf("User 1002 score: %d\n", score)MapGetByKeyListOp: get multiple values by key list
Retrieves values for multiple keys in a single operation.
// Get multiple values by key listkeys := []any{"name", "email", "age"}record, err := client.Operate(nil, key, as.MapGetByKeyListOp("profile", keys, as.MapReturnType.VALUE),)
values := record.Bins["profile"].([]any)// values[0] = name, values[1] = email, values[2] = ageMapGetByKeyRangeOp: get values by key range
Retrieves values for keys within a specified range. Works with ordered maps.
// Get values for keys in range [startKey, endKey)record, err := client.Operate(nil, key, as.MapGetByKeyRangeOp("products", "A", "D", as.MapReturnType.KEY_VALUE),)
products := record.Bins["products"].([]as.MapPair) // Products with keys A, B, CMapGetByIndexOp: get by index position
Retrieves a value by its index position in the map (for ordered maps).
// Get first item (index 0)record, err := client.Operate(nil, key, as.MapGetByIndexOp("leaderboard", 0, as.MapReturnType.KEY_VALUE),)
topPlayers := record.Bins["leaderboard"].([]as.MapPair)playerAtIndex := topPlayers[0]MapGetByRankOp: get by value rank
Retrieves a value by its rank (sorted by value). Rank 0 is the smallest value, rank -1 is the largest value (highest rank).
// Get item with lowest value (rank 0)record, err := client.Operate(nil, key, as.MapGetByRankOp("scores", 0, as.MapReturnType.KEY_VALUE),)
// Get item with highest value (rank -1)record, err := client.Operate(nil, key, as.MapGetByRankOp("scores", -1, as.MapReturnType.KEY_VALUE),)
results := record.Bins["scores"].([]as.MapPair)topPlayer := results[0]fmt.Printf("Top player: %v with score %v\n", topPlayer.Key, topPlayer.Value)Understanding ranks:
- Rank 0: Smallest/lowest value
- Rank 1: Second smallest value
- Rank -1: Largest/highest value (most common for leaderboards)
- Rank -2: Second largest value
MapGetByValueOp: get keys by value
Finds all keys that have a specific value.
// Find all users with score 100record, err := client.Operate(nil, key, as.MapGetByValueOp("scores", 100, as.MapReturnType.KEY),)
userIds := record.Bins["scores"].([]any) // All users with score 100MapSizeOp: get map size
Returns the number of items in the map.
// Get map sizerecord, err := client.Operate(nil, key, as.MapSizeOp("profile"),)
size := record.Bins["profile"].(int)fmt.Printf("Profile has %d fields\n", size)