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.
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(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
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)
}
}
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(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
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)
}
}
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(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
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
$push,$each,$pullAll,$addToSet,$pop, and$positionaren’t supported.