Update operators for tables (Go)
Some Data API commands, like Update a row (Go), use update operators to modify rows in a table.
Set
The $set operator sets the value of the specified column to the specified value.
Example setting a non-map column:
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/update"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update a row
err := table.UpdateOne(
ctx,
filter.And(
filter.Eq("title", "Hidden Shadows of the Past"),
filter.Eq("author", "John Anthony"),
),
update.Table().
Set("rating", 4.5).
Set("genres", []string{"Fiction", "Drama"}),
)
if err != nil {
log.Fatal(err)
}
}
Example setting a map column:
If the updated map includes non-string keys, you must use a slice of key-value pairs to update the map column.
Otherwise, you can use a slice of key-value pairs or a map.
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/update"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update a row
err := table.UpdateOne(
ctx,
filter.And(
filter.Eq("title", "Hidden Shadows of the Past"),
filter.Eq("author", "John Anthony"),
),
update.Table().
// This map has non-string keys,
// so the update is a slice of key-value pairs
Set("map_column_int_str", [][]any{{1, "value1"}, {2, "value2"}}).
// This map does not have non-string keys,
// so the update does not need to be a slice of key-value
// pairs
Set("map_column_str_str", map[string]any{"key1": "value1", "key2": "value2"}),
)
if err != nil {
log.Fatal(err)
}
}
Unset
The $unset operator sets the specified column’s value to null or the equivalent empty form, such as [] or {} for map, list, and set types.
Unsetting a column produces a tombstone. Excessive tombstones can impact query performance.
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/update"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update a row
err := table.UpdateOne(
ctx,
filter.And(
filter.Eq("title", "Hidden Shadows of the Past"),
filter.Eq("author", "John Anthony"),
),
update.Table().Unset("genres"),
)
if err != nil {
log.Fatal(err)
}
}
Push
The $push operator appends a single element to a map, list, or set.
To append multiple items, use the $each operator with $push.
Example appending to a list or set column:
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/update"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update a row
err := table.UpdateOne(
ctx,
filter.And(
filter.Eq("title", "Hidden Shadows of the Past"),
filter.Eq("author", "John Anthony"),
),
update.Table().
Push("genres", "SciFi").
PushEach("topics", "robots", "AI"),
)
if err != nil {
log.Fatal(err)
}
}
Example appending to a map column:
When appending to a map, if the appended key-value pair has a non-string key, you must represent the key-value pair as a slice. Otherwise, you can either use a slice or a map.
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/update"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update a row
err := table.UpdateOne(
ctx,
filter.And(
filter.Eq("title", "Hidden Shadows of the Past"),
filter.Eq("author", "John Anthony"),
),
update.Table().
// This update includes non-string keys,
// so the update is a key-value pair represented as a slice
Push("map_column_int_str", []any{1, "value1"}).
// This update does not include non-string keys,
// so the update can be a key-value pair represented as a
// slice or a map
Push("map_column_str_str", map[string]any{"key1": "value1"}).
// When using $each, use a slice of key-value pairs for
// non-string keys
PushEach("map_column_int_str_2", []any{1, "value1"}, []any{2, "value2"}).
// When using $each, use a slice of key-value pairs or maps
// for string keys
PushEach("map_column_str_str_2", map[string]any{"key1": "value1"}, []any{"key2", "value2"}),
)
if err != nil {
log.Fatal(err)
}
}
Each
The $each operator modifies the $push operator to append multiple items to a map, list, or set.
Example appending to a list or set column:
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/update"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update a row
err := table.UpdateOne(
ctx,
filter.And(
filter.Eq("title", "Hidden Shadows of the Past"),
filter.Eq("author", "John Anthony"),
),
update.Table().
Push("genres", "SciFi").
PushEach("topics", "robots", "AI"),
)
if err != nil {
log.Fatal(err)
}
}
Example appending to a map column:
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/update"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update a row
err := table.UpdateOne(
ctx,
filter.And(
filter.Eq("title", "Hidden Shadows of the Past"),
filter.Eq("author", "John Anthony"),
),
update.Table().
// This update includes non-string keys,
// so the update is a key-value pair represented as a slice
Push("map_column_int_str", []any{1, "value1"}).
// This update does not include non-string keys,
// so the update can be a key-value pair represented as a
// slice or a map
Push("map_column_str_str", map[string]any{"key1": "value1"}).
// When using $each, use a slice of key-value pairs for
// non-string keys
PushEach("map_column_int_str_2", []any{1, "value1"}, []any{2, "value2"}).
// When using $each, use a slice of key-value pairs or maps
// for string keys
PushEach("map_column_str_str_2", map[string]any{"key1": "value1"}, []any{"key2", "value2"}),
)
if err != nil {
log.Fatal(err)
}
}
Pull all
The $pullAll operator removes the specified elements from a list or set, or removes entries that match the specified keys from a map.
Example removing matches from a list or set column:
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/update"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update a row
err := table.UpdateOne(
ctx,
filter.And(
filter.Eq("title", "Hidden Shadows of the Past"),
filter.Eq("author", "John Anthony"),
),
update.Table().PullAll("genres", "SciFi", "Romance"),
)
if err != nil {
log.Fatal(err)
}
}
Example removing matches from map column:
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/update"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update a row
err := table.UpdateOne(
ctx,
filter.And(
filter.Eq("title", "Hidden Shadows of the Past"),
filter.Eq("author", "John Anthony"),
),
update.Table().PullAll("metadata", "language", "edition"),
)
if err != nil {
log.Fatal(err)
}
}
Combine operators
You can use multiple update operators in a single update.
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/update"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update a row
err := table.UpdateOne(
ctx,
filter.And(
filter.Eq("title", "Hidden Shadows of the Past"),
filter.Eq("author", "John Anthony"),
),
update.Table().
Set("rating", 4.5).
Set("genres", []string{"Fiction", "Drama"}).
Unset("borrower"),
)
if err != nil {
log.Fatal(err)
}
}
Unsupported operators
-
$renameisn’t supported. Instead, usealterTableto add and drop columns. -
$currentDateisn’t supported. -
The array operators
$addToSet,$pop, and$positionaren’t supported.