Update a row (Go)
|
Tables with the Data API are currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms. |
Updates a single row in a table.
If the row does not already exist and the update includes at least one non-null or non-empty value, creates a new row.
For general information about working with tables and rows, see About tables with the Data API (Go).
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Updates the specified row.
If no row matches the specified primary key and the update includes at least one non-null or non-empty value, then a new row is created with the values specified $set values and primary key values.
Any omitted or $unset columns are set to null in the new row.
A successful operation does not return anything.
|
A rare edge case, related to underlying Apache Cassandra® functionality, can cause a row to disappear altogether when all of its columns are set to This happens if the row was previously created from an update operation that had no pre-existing row to modify. |
Parameters
Use the UpdateOne method, which belongs to the Table type.
Method signature
func (t *Table) UpdateOne(
ctx context.Context,
f TableFilter,
u TableUpdate,
opts ...options.TableUpdateOneOption
) error
| Name | Type | Summary |
|---|---|---|
|
|
The context for the operation. |
|
|
Describes the full primary key of the row to update. For this method, the filter can only use the |
|
|
Defines the update using Data API operators. For a list of available operators and more examples, see Update operators for tables (Go). You cannot update primary key values. If you need to modify a row’s primary key, delete the row and then insert a new row with the desired primary key values. |
|
Optional.
A builder to generate options for this operation.
See Methods of the |
| Method | Summary |
|---|---|
|
Optional. General API options for this operation, including the timeout. |
Examples
The following examples demonstrate how to update a row in a table.
Update multiple columns
You can combine multiple operators and properties in a single call. For the full list of operators, see Update operators for tables (Go).
If the row does not already exist and the update includes at least one non-null or non-empty value, creates a new row.
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)
}
}
Unset columns
To unset a column, you can use the $unset operator, or you can use the $set operator and an empty value.
Either operation will delete the value in the specified column.
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)
}
}
Client reference
For more information, see the client reference.