Update a row
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.
|
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
-
Python
-
TypeScript
-
Go
-
Java
-
C#
-
curl
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.
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. |
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.
Returns a promise that resolves once the operation completes.
|
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. |
|
The Go client is in preview. For more information, see astra-db-go. |
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. |
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.
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. |
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 specified $set values and primary key values.
Any omitted or $unset columns are set to null in the new row.
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. |
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 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. |
Always returns a status.matchedCount of 1, a status.modifiedCount of 1, and a status.upsertCount of 0, regardless of the outcome.
Example response:
{
"status": {
"matchedCount": 1,
"modifiedCount": 1,
"upsertCount": 0
}
}
Parameters
-
Python
-
TypeScript
-
Go
-
Java
-
C#
-
curl
Use the update_one method, which belongs to the astrapy.Table class.
Method signature
update_one(
filter: Dict[str, Any],
update: Dict[str, Any],
*,
general_method_timeout_ms: int,
request_timeout_ms: int,
timeout_ms: int,
) -> None
| Name | Type | Summary |
|---|---|---|
|
|
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. 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. The maximum time, in milliseconds, that the client should wait for the underlying HTTP request. This parameter is aliased as Default: The default value for the table. This default is 30 seconds unless you specified a different default when you initialized the |
Use the updateOne method, which belongs to the Table class.
Method signature
async updateOne(
filter: TableFilter<Schema>,
update: TableUpdateFilter<Schema>,
options?: {
timeout?: number | TimeoutDescriptor,
},
): void
| Name | Type | Summary |
|---|---|---|
|
|
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. 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 timeout to impose on the underlying API request. |
|
The Go client is in preview. For more information, see astra-db-go. |
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. 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. |
Use the updateOne method, which belongs to the com.datastax.astra.client.tables.Table class.
Method signature
void updateOne(
Filter filter,
TableUpdateOperation update
)
void updateOne(
Filter filter,
TableUpdateOperation update,
TableUpdateOneOptions options
)
| Name | Type | Summary |
|---|---|---|
|
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. 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. |
|
|
General API options for this operation, including the timeout. |
Use the UpdateOneAsync method, which belongs to the Table class.
You can also use UpdateOne, which is the synchronous version of the method.
Method signature
public Task UpdateOneAsync(
TableFilter<T> filter,
UpdateBuilder<T> update,
TableUpdateOneOptions<T> options = null
);
| Name | Type | Summary |
|---|---|---|
|
|
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. 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. General API options for this operation, including the timeout. For more information and examples, see Customize API interaction. |
Use the updateOne command.
Command signature
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": FILTER,
"update": UPDATE
}
}'
| Name | Type | Summary |
|---|---|---|
|
|
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. 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. |
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.
If the row does not already exist and the update includes at least one non-null or non-empty value, creates a new row.
-
Python
-
TypeScript
-
Go
-
Java
-
C#
-
curl
The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
table = database.get_table("TABLE_NAME")
# Update a row
table.update_one(
{"title": "Hidden Shadows of the Past", "author": "John Anthony"},
{
"$set": {"rating": 4.5, "genres": ["Fiction", "Drama"]},
"$unset": {"borrower": ""},
},
)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
const table = database.table("TABLE_NAME");
// Update a row
(async function () {
await table.updateOne(
{
title: "Hidden Shadows of the Past",
author: "John Anthony",
},
{
$set: {
rating: 4.5,
genres: ["Fiction", "Drama"],
},
$unset: {
borrower: "",
},
},
);
})();
|
The Go client is in preview. For more information, see astra-db-go. |
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() {
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update rows
err := table.UpdateOne(
context.Background(),
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)
}
}
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.TableUpdateOperation;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Arrays;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Update a row
Filter filter =
Filters.and(
Filters.eq("title", "Hidden Shadows of the Past"),
Filters.eq("author", "John Anthony"));
TableUpdateOperation update =
new TableUpdateOperation()
.set("rating", 4.5)
.set("genres", Arrays.asList("Fiction", "Drama"))
.unset("borrower");
table.updateOne(filter, update);
}
}
-
Typed
-
Untyped
You can manually define a client-side type for your table to help statically catch errors. For more information and examples, see Custom typing for tables.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Book
{
[ColumnPrimaryKey(1)]
[ColumnName("title")]
public string Title { get; set; } = null!;
[ColumnPrimaryKey(2)]
[ColumnName("author")]
public string Author { get; set; } = null!;
[ColumnName("rating")]
public float? Rating { get; set; }
[ColumnName("genres")]
public HashSet<string>? Genres { get; set; }
[ColumnName("borrower")]
public string? Borrower { get; set; }
}
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable<Book>("TABLE_NAME");
// Update a row
var filterBuilder = Builders<Book>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq(b => b.Title, "Hidden Shadows of the Past"),
filterBuilder.Eq(b => b.Author, "John Anthony")
);
var update = Builders<Book>
.TableUpdate.Set(x => x.Rating, 4.5f)
.Set(x => x.Genres, new HashSet<string> { "Fiction", "Drama" })
.Unset(x => x.Borrower);
await table.UpdateOneAsync(filter, update);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable("TABLE_NAME");
// Update a row
var filterBuilder = Builders<Row>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq("title", "Hidden Shadows of the Past"),
filterBuilder.Eq("author", "John Anthony")
);
var update = Builders<Row>
.TableUpdate.Set("rating", 4.5)
.Set("genres", new HashSet<string> { "Fiction", "Drama" })
.Unset("borrower");
await table.UpdateOneAsync(filter, update);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$set": {
"rating": 4.5,
"genres": ["Fiction", "Drama"]
},
"$unset": {
"borrower": ""
}
}
}
}'
Update a map column
-
Python
-
TypeScript
-
Go
-
Java
-
C#
-
curl
If the updated map includes non-string keys, you must use an array of key-value pairs to update the map column.
With the Python client, you can also use DataAPIMap to encode maps that use non-string keys.
Otherwise, you can use an array of key-value pairs or a normal map.
from astrapy import DataAPIClient
from astrapy.data_types import DataAPIMap
# Get an existing table
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
table = database.get_table("TABLE_NAME")
# Update a row
table.update_one(
{"title": "Hidden Shadows of the Past", "author": "John Anthony"},
{
"$set": {
# This map has non-string keys,
# so the update is an array of key-value pairs
"map_column_int_str": [[1, "value1"], [2, "value2"]],
# Alternatively, use DataAPIMap to encode maps with non-string keys
"map_column_int_str_2": DataAPIMap(
{1: "value1", 2: "value2"}
),
# This map does not have non-string keys,
# so the update does not need to be an array of key-value pairs
"map_column_str_str": {"key1": "value1", "key2": "value2"},
}
},
)
If the updated map includes non-string keys, you must use an array of key-value pairs to update the map column.
Otherwise, you can use an array of key-value pairs or a normal map.
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
const table = database.table("TABLE_NAME");
// Update a row
(async function () {
await table.updateOne(
{
title: "Hidden Shadows of the Past",
author: "John Anthony",
},
{
$set: {
// This map has non-string keys,
// so the update is an array of key-value pairs
map_column_int_str: [
[1, "value1"],
[2, "value2"],
],
// This map does not have non-string keys,
// so the update does not need to be an array of key-value pairs
map_column_str_str: {
key1: "value1",
key2: "value2",
},
},
},
);
})();
|
The Go client is in preview. For more information, see astra-db-go. |
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 normal 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() {
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update rows
err := table.UpdateOne(
context.Background(),
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)
}
}
The Java client supports updates of a row with a map column that includes non-string keys. (You don’t need to use an array of key-value pairs to represent the map column.)
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.TableUpdateOperation;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Map;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
Filter filter =
Filters.and(
Filters.eq("title", "Hidden Shadows of the Past"),
Filters.eq("author", "John Anthony"));
// This map has non-string keys,
// but the insertion can still be represented as a map
// instead of an array of key-value pairs
Map<Integer, String> mapColumn1 = Map.of(1, "value1", 2, "value2");
// This map does not have non-string keys
Map<String, String> mapColumn2 = Map.of("key1", "value1", "key2", "value2");
TableUpdateOperation update =
new TableUpdateOperation()
.set("map_column_int_str", mapColumn1)
.set("map_column_str_str", mapColumn2);
table.updateOne(filter, update);
}
}
The C# client supports updates of a row with a map column that includes non-string keys. (You don’t need to use an array of key-value pairs to represent the map column.)
-
Typed
-
Untyped
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Book
{
[ColumnPrimaryKey(1)]
[ColumnName("title")]
public string Title { get; set; } = null!;
[ColumnPrimaryKey(2)]
[ColumnName("author")]
public string Author { get; set; } = null!;
[ColumnName("map_column_int_str")]
public Dictionary<int, string>? MapColumnIntStr { get; set; }
[ColumnName("map_column_str_str")]
public Dictionary<string, string>? MapColumnStrStr { get; set; }
}
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable<Book>("TABLE_NAME");
var mapColumnIntStr = new Dictionary<int, string>
{
{ 1, "value1" },
{ 2, "value2" },
};
var mapColumnStrStr = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" },
};
var filterBuilder = Builders<Book>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq(b => b.Title, "Hidden Shadows of the Past"),
filterBuilder.Eq(b => b.Author, "John Anthony")
);
var update = Builders<Book>
.TableUpdate.Set(b => b.MapColumnIntStr, mapColumnIntStr)
.Set(b => b.MapColumnStrStr, mapColumnStrStr);
await table.UpdateOneAsync(filter, update);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable("TABLE_NAME");
var mapColumnIntStr = new Dictionary<int, string>
{
{ 1, "value1" },
{ 2, "value2" },
};
var mapColumnStrStr = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" },
};
var filterBuilder = Builders<Row>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq("title", "Hidden Shadows of the Past"),
filterBuilder.Eq("author", "John Anthony")
);
var update = Builders<Row>
.TableUpdate.Set("map_column_int_str", mapColumnIntStr)
.Set("map_column_str_str", mapColumnStrStr);
await table.UpdateOneAsync(filter, update);
}
}
If the updated map includes non-string keys, you must use an array of key-value pairs to update the map column.
Otherwise, you can use an array of key-value pairs or a normal map.
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$set": {
"map_column_int_str": [
[1, "value1"],
[2, "value2"]
],
"map_column_str_str": {
"key1": "value1",
"key2": "value2"
}
}
}
}
}'
Append to a list or set column
Use $push to append a single element to a list or set.
Use $push with $each to append multiple elements.
-
Python
-
TypeScript
-
Go
-
Java
-
C#
-
curl
The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
table = database.get_table("TABLE_NAME")
# Update a row
table.update_one(
{"title": "Hidden Shadows of the Past", "author": "John Anthony"},
{
"$push": {
# Appends a single element to the "genres" list
"genres": "SciFi",
# Appends two elements to the "topics" list
"topics": {"$each": ["robots", "AI"]},
}
},
)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
const table = database.table("TABLE_NAME");
// Update a row
(async function () {
await table.updateOne(
{
title: "Hidden Shadows of the Past",
author: "John Anthony",
},
{
$push: {
// Appends a single element to the "genres" list
genres: "SciFi",
// Appends two elements to the "topics" list
topics: {
$each: ["robots", "AI"],
},
},
},
);
})();
|
The Go client is in preview. For more information, see astra-db-go. |
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() {
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update rows
err := table.UpdateOne(
context.Background(),
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)
}
}
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.TableUpdateOperation;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Arrays;
import java.util.Map;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Update a row
Filter filter =
Filters.and(
Filters.eq("title", "Hidden Shadows of the Past"),
Filters.eq("author", "John Anthony"));
TableUpdateOperation update =
new TableUpdateOperation(
Map.of(
"$push",
Map.of(
// Appends a single element to the "genres" list
"genres",
"SciFi",
// Appends two elements to the "topics" list
"topics",
Map.of("$each", Arrays.asList("robots", "AI")))));
table.updateOne(filter, update);
}
}
-
Typed
-
Untyped
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Book
{
[ColumnPrimaryKey(1)]
[ColumnName("title")]
public string Title { get; set; } = null!;
[ColumnPrimaryKey(2)]
[ColumnName("author")]
public string Author { get; set; } = null!;
[ColumnName("genres")]
public HashSet<string>? Genres { get; set; }
[ColumnName("topics")]
public HashSet<string>? Topics { get; set; }
}
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable<Book>("TABLE_NAME");
// Update a row
var filterBuilder = Builders<Book>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq(b => b.Title, "Hidden Shadows of the Past"),
filterBuilder.Eq(b => b.Author, "John Anthony")
);
var update = Builders<Book>
.TableUpdate.Push(x => x.Genres, "SciFi")
.PushEach(x => x.Topics, ["robots", "AI"]);
await table.UpdateOneAsync(filter, update);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable("TABLE_NAME");
var filterBuilder = Builders<Row>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq("title", "Hidden Shadows of the Past"),
filterBuilder.Eq("author", "John Anthony")
);
var update = Builders<Row>
.TableUpdate.Push("genres", "SciFi")
.PushEach("topics", ["robots", "AI"]);
await table.UpdateOneAsync(filter, update);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$push": {
"genres": "SciFi",
"topics": {
"$each": ["robots", "AI"]
}
}
}
}
}'
Append to a map column
Use $push to append a single key-value pair to a map.
Use $push with $each to append multiple key-value pairs.
-
Python
-
TypeScript
-
Go
-
Java
-
C#
-
curl
If the appended key-value pair has a non-string key, you must represent the key-value pair as an array. Otherwise, you can either use an array or a normal map.
The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.
from astrapy import DataAPIClient
from astrapy.data_types import DataAPIMap
# Get an existing table
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
table = database.get_table("TABLE_NAME")
# Update a row
table.update_one(
{"title": "Hidden Shadows of the Past", "author": "John Anthony"},
{
"$push": {
# This update includes non-string keys,
# so the update is a key-value pair represented as an array
"map_column_int_str": [1, "value1"],
# This update does not include non-string keys,
# so the update can be a key-value pair represented as an array or a map
"map_column_str_str": {"key1": "value1"},
# When using $each, use an array of key-value pairs for non-string keys
"map_column_int_str_2": {
"$each": [[1, "value1"], [2, "value2"]]
},
# When using $each, use an array of key-value pairs or maps for string keys
"map_column_str_str_2": {
"$each": [{"key1": "value1"}, ["key2", "value2"]]
},
}
},
)
If the appended key-value pair has a non-string key, you must represent the key-value pair as an array. Otherwise, you can either use an array or a normal map.
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
const table = database.table("TABLE_NAME");
// Update a row
(async function () {
await table.updateOne(
{
title: "Hidden Shadows of the Past",
author: "John Anthony",
},
{
$push: {
// This update includes non-string keys,
// so the update is a key-value pair represented as an array
map_column_int_str: [1, "value1"],
// This update does not include non-string keys,
// so the update can be a key-value pair represented as an array or a map
map_column_str_str: {
key1: "value1",
},
// When using $each, use an array of key-value pairs for non-string keys
map_column_int_str_2: {
$each: [
[1, "value1"],
[2, "value2"],
],
},
// When using $each, use an array of key-value pairs or maps for string keys
map_column_str_str_2: {
$each: [{ key1: "value1" }, ["key2", "value2"]],
},
},
},
);
})();
|
The Go client is in preview. For more information, see astra-db-go. |
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 normal 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() {
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update rows
err := table.UpdateOne(
context.Background(),
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)
}
}
If the appended key-value pair has a non-string key, you must represent the key-value pair as an array. Otherwise, you can either use an array or a normal map.
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.TableUpdateOperation;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Arrays;
import java.util.Map;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Update a row
Filter filter =
Filters.and(
Filters.eq("title", "Hidden Shadows of the Past"),
Filters.eq("author", "John Anthony"));
TableUpdateOperation update =
new TableUpdateOperation(
Map.of(
"$push",
Map.of(
// This update includes non-string keys,
// so the update is a key-value pair represented as an array
"map_column_int_str",
Arrays.asList(1, "value1"),
// This update does not include non-string keys,
// so the update can be a key-value pair represented as an array or a map
"map_column_str_str",
Map.of("key1", "value1"),
// When using $each, use an array of key-value pairs for non-string keys
"map_column_int_str_2",
Map.of(
"$each",
Arrays.asList(Arrays.asList(1, "value1"), Arrays.asList(2, "value2"))),
// When using $each, use an array of key-value pairs or maps for string keys
"map_column_str_str_2",
Map.of(
"$each",
Arrays.asList(
Map.of("key1", "value1"), Arrays.asList("key2", "value2"))))));
table.updateOne(filter, update);
}
}
-
Typed
-
Untyped
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Book
{
[ColumnPrimaryKey(1)]
[ColumnName("title")]
public string Title { get; set; } = null!;
[ColumnPrimaryKey(2)]
[ColumnName("author")]
public string Author { get; set; } = null!;
[ColumnName("map_column_int_str")]
public Dictionary<int, string>? MapColumnIntStr { get; set; }
[ColumnName("map_column_str_str")]
public Dictionary<string, string>? MapColumnStrStr { get; set; }
[ColumnName("map_column_int_str_2")]
public Dictionary<int, string>? MapColumnIntStr2 { get; set; }
[ColumnName("map_column_str_str_2")]
public Dictionary<string, string>? MapColumnStrStr2 { get; set; }
}
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable<Book>("TABLE_NAME");
var filterBuilder = Builders<Book>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq(b => b.Title, "Hidden Shadows of the Past"),
filterBuilder.Eq(b => b.Author, "John Anthony")
);
var update = Builders<Book>
.TableUpdate.Push(
b => b.MapColumnIntStr,
new Dictionary<int, string> { { 1, "value1" } }
)
.Push(
b => b.MapColumnStrStr,
new Dictionary<string, string> { { "key1", "value1" } }
)
.PushEach(
b => b.MapColumnIntStr2,
new Dictionary<int, string> { { 1, "value1" }, { 2, "value2" } }
)
.PushEach(
b => b.MapColumnStrStr2,
new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" },
}
);
await table.UpdateOneAsync(filter, update);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable("TABLE_NAME");
var filterBuilder = Builders<Row>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq("title", "Hidden Shadows of the Past"),
filterBuilder.Eq("author", "John Anthony")
);
var update = Builders<Row>
.TableUpdate.Push(
"map_column_int_str",
new Dictionary<int, string> { { 1, "value1" } }
)
.Push(
"map_column_str_str",
new Dictionary<string, string> { { "key1", "value1" } }
)
.PushEach(
"map_column_int_str_2",
new Dictionary<int, string> { { 1, "value1" }, { 2, "value2" } }
)
.PushEach(
"map_column_str_str_2",
new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" },
}
);
await table.UpdateOneAsync(filter, update);
}
}
If the appended key-value pair has a non-string key, you must represent the key-value pair as an array. Otherwise, you can either use an array or a normal map.
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$push": {
"map_column_int_str": [
1, "value1"
],
"map_column_str_str": {
"key1": "value1"
},
"map_column_int_str_2": {
"$each": [
[1, "value1"],
[2, "value2"]
]
},
"map_column_str_str_2": {
"$each": [
{"key1": "value1"},
["key2", "value2"]
]
}
}
}
}
}'
Remove matches from a list or set column
Use $pullAll to remove the specified elements from a list or set.
-
Python
-
TypeScript
-
Go
-
Java
-
C#
-
curl
The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
table = database.get_table("TABLE_NAME")
# Update a row
table.update_one(
{"title": "Hidden Shadows of the Past", "author": "John Anthony"},
{
"$pullAll": {
"genres": ["SciFi", "Romance"],
}
},
)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
const table = database.table("TABLE_NAME");
// Update a row
(async function () {
await table.updateOne(
{
title: "Hidden Shadows of the Past",
author: "John Anthony",
},
{
$pullAll: {
genres: ["SciFi", "Romance"],
},
},
);
})();
|
The Go client is in preview. For more information, see astra-db-go. |
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() {
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update rows
err := table.UpdateOne(
context.Background(),
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)
}
}
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.TableUpdateOperation;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Arrays;
import java.util.Map;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Update a row
Filter filter =
Filters.and(
Filters.eq("title", "Hidden Shadows of the Past"),
Filters.eq("author", "John Anthony"));
TableUpdateOperation update =
new TableUpdateOperation(
Map.of("$pullAll", Map.of("genres", Arrays.asList("SciFi", "Romance"))));
table.updateOne(filter, update);
}
}
-
Typed
-
Untyped
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Book
{
[ColumnPrimaryKey(1)]
[ColumnName("title")]
public string Title { get; set; } = null!;
[ColumnPrimaryKey(2)]
[ColumnName("author")]
public string Author { get; set; } = null!;
[ColumnName("genres")]
public HashSet<string>? Genres { get; set; }
}
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable<Book>("TABLE_NAME");
// Update a row
var filterBuilder = Builders<Book>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq(b => b.Title, "Hidden Shadows of the Past"),
filterBuilder.Eq(b => b.Author, "John Anthony")
);
var update = Builders<Book>.TableUpdate.PullAll(
x => x.Genres,
new[] { "SciFi", "Romance" }
);
await table.UpdateOneAsync(filter, update);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable("TABLE_NAME");
// Update a row
var filterBuilder = Builders<Row>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq("title", "Hidden Shadows of the Past"),
filterBuilder.Eq("author", "John Anthony")
);
var update = Builders<Row>.TableUpdate.PullAll(
"genres",
new[] { "SciFi", "Romance" }
);
await table.UpdateOneAsync(filter, update);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$pullAll": {
"genres": ["SciFi", "Romance"]
}
}
}
}'
Remove matches from map column
Use $pullAll to remove entries that match the specified keys from a map column.
-
Python
-
TypeScript
-
Go
-
Java
-
C#
-
curl
The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
table = database.get_table("TABLE_NAME")
# Update a row
table.update_one(
{"title": "Hidden Shadows of the Past", "author": "John Anthony"},
{
"$pullAll": {
"metadata": ["language", "edition"],
}
},
)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
const table = database.table("TABLE_NAME");
// Update a row
(async function () {
await table.updateOne(
{
title: "Hidden Shadows of the Past",
author: "John Anthony",
},
{
$pullAll: {
metadata: ["language", "edition"],
},
},
);
})();
|
The Go client is in preview. For more information, see astra-db-go. |
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() {
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update rows
err := table.UpdateOne(
context.Background(),
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)
}
}
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.TableUpdateOperation;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Arrays;
import java.util.Map;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Update a row
Filter filter =
Filters.and(
Filters.eq("title", "Hidden Shadows of the Past"),
Filters.eq("author", "John Anthony"));
TableUpdateOperation update =
new TableUpdateOperation(
Map.of("$pullAll", Map.of("metadata", Arrays.asList("language", "edition"))));
table.updateOne(filter, update);
}
}
-
Typed
-
Untyped
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Book
{
[ColumnPrimaryKey(1)]
[ColumnName("title")]
public string Title { get; set; } = null!;
[ColumnPrimaryKey(2)]
[ColumnName("author")]
public string Author { get; set; } = null!;
[ColumnName("metadata")]
public Dictionary<string, string>? Metadata { get; set; }
}
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable<Book>("TABLE_NAME");
// Update a row
var filterBuilder = Builders<Book>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq(b => b.Title, "Hidden Shadows of the Past"),
filterBuilder.Eq(b => b.Author, "John Anthony")
);
var update = Builders<Book>.TableUpdate.PullAll(
x => x.Metadata,
new[] { "language", "edition" }
);
await table.UpdateOneAsync(filter, update);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable("TABLE_NAME");
// Update a row
var filterBuilder = Builders<Row>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq("title", "Hidden Shadows of the Past"),
filterBuilder.Eq("author", "John Anthony")
);
var update = Builders<Row>.TableUpdate.PullAll(
"metadata",
new[] { "language", "edition" }
);
await table.UpdateOneAsync(filter, update);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$pullAll": {
"metadata": ["language", "edition"]
}
}
}
}'
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.
-
Python
-
TypeScript
-
Go
-
Java
-
C#
-
curl
The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
table = database.get_table("TABLE_NAME")
# Update a row
table.update_one(
{"title": "Hidden Shadows of the Past", "author": "John Anthony"},
{"$unset": {"genres": ""}},
)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
const table = database.table("TABLE_NAME");
// Update a row
(async function () {
await table.updateOne(
{
title: "Hidden Shadows of the Past",
author: "John Anthony",
},
{
$unset: {
genres: "",
},
},
);
})();
|
The Go client is in preview. For more information, see astra-db-go. |
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() {
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update rows
err := table.UpdateOne(
context.Background(),
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)
}
}
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.TableUpdateOperation;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Update a row
Filter filter =
Filters.and(
Filters.eq("title", "Hidden Shadows of the Past"),
Filters.eq("author", "John Anthony"));
TableUpdateOperation update = new TableUpdateOperation().unset("genres");
table.updateOne(filter, update);
}
}
-
Typed
-
Untyped
You can manually define a client-side type for your table to help statically catch errors. For more information and examples, see Custom typing for tables.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Book
{
[ColumnPrimaryKey(1)]
[ColumnName("title")]
public string Title { get; set; } = null!;
[ColumnPrimaryKey(2)]
[ColumnName("author")]
public string Author { get; set; } = null!;
[ColumnName("number_of_pages")]
public int? NumberOfPages { get; set; }
[ColumnName("genres")]
public HashSet<string>? Genres { get; set; }
[ColumnName("due_date")]
public DateOnly? DueDate { get; set; }
}
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable<Book>("TABLE_NAME");
// Update a row
var filterBuilder = Builders<Book>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq(b => b.Title, "Hidden Shadows of the Past"),
filterBuilder.Eq(b => b.Author, "John Anthony")
);
var update = Builders<Book>.TableUpdate.Unset(x => x.Genres);
await table.UpdateOneAsync(filter, update);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable("TABLE_NAME");
// Update a row
var filterBuilder = Builders<Row>.TableFilter;
var filter = filterBuilder.And(
filterBuilder.Eq("title", "Hidden Shadows of the Past"),
filterBuilder.Eq("author", "John Anthony")
);
var update = Builders<Row>.TableUpdate.Unset("genres");
await table.UpdateOneAsync(filter, update);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Hidden Shadows of the Past",
"author": "John Anthony"
},
"update": {
"$unset": {
"genres": ""
}
}
}
}'
Update a user-defined type column
If a column is a user-defined type, you must update the whole value.
You can’t update a partial value.
In the example below, president and vice_president both use the same user-defined type, which consists of an email and user_name field.
If the value type of a map, list, or set column is a user-defined type, you can only update the whole value, not a partial value, for each entry.
The $push, $each, and $pullAll operators work on map, list, and set columns that have a user-defined type as their value type.
For examples, see Update a map column, Append to a list or set column, Append to a map column, Remove matches from a list or set column, and Remove matches from map column.
-
Python
-
TypeScript
-
Go
-
Java
-
C#
-
curl
The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
table = database.get_table("TABLE_NAME")
# Update a row
table.update_one(
{"title": "Chemistry Club"},
{
"$set": {
"president": {
"email": "lisa@example.com",
"user_name": "lisa_m",
},
"vice_president": {
"email": "tanya@example.com",
"user_name": "tanya_o",
},
},
},
)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
const table = database.table("TABLE_NAME");
// Update a row
(async function () {
await table.updateOne(
{
title: "Chemistry Club",
},
{
$set: {
president: {
email: "lisa@example.com",
user_name: "lisa_m",
},
vice_president: {
email: "tanya@example.com",
user_name: "tanya_o",
},
},
},
);
})();
|
The Go client is in preview. For more information, see astra-db-go. |
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() {
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Update rows
err := table.UpdateOne(
context.Background(),
filter.Eq("title", "Chemistry Club"),
update.Table().Set("president", map[string]any{
"email": "lisa@example.com",
"user_name": "lisa_m"}).Set("vice_president", map[string]any{
"email": "tanya@example.com",
"user_name": "tanya_o"}),
)
if err != nil {
log.Fatal(err)
}
}
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.TableUpdateOperation;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Map;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Update a row
Filter filter = Filters.eq("title", "Chemistry Club");
TableUpdateOperation update =
new TableUpdateOperation()
.set("president", Map.of("user_name", "lisa_m", "email", "lisa@example.com"))
.set("vice_president", Map.of("user_name", "tanya_o", "email", "tanya@example.com"));
table.updateOne(filter, update);
}
}
-
Typed
-
Untyped
You can manually define a client-side type for your table to help statically catch errors. For more information and examples, see Custom typing for tables.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
[UserDefinedType("example_udt")]
public class ExampleUDT
{
[ColumnName("email")]
public string? Email { get; set; }
[ColumnName("user_name")]
public string? UserName { get; set; }
};
public class ExampleRow
{
[ColumnPrimaryKey]
[ColumnName("title")]
public string Title { get; set; } = null!;
[ColumnName("president")]
public ExampleUDT? President { get; set; }
[ColumnName("vice_president")]
public ExampleUDT? VicePresident { get; set; }
}
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable<ExampleRow>("TABLE_NAME");
// Update a row
var filter = Builders<ExampleRow>.TableFilter.Eq(
x => x.Title,
"Chemistry Club"
);
var update = Builders<ExampleRow>
.TableUpdate.Set(
x => x.President,
new ExampleUDT { Email = "lisa@example.com", UserName = "lisa_m" }
)
.Set(
x => x.VicePresident,
new ExampleUDT
{
Email = "tanya@example.com",
UserName = "tanya_o",
}
);
await table.UpdateOneAsync(filter, update);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var table = database.GetTable("TABLE_NAME");
// Update a row
var filter = Builders<Row>.TableFilter.Eq("title", "Chemistry Club");
var update = Builders<Row>
.TableUpdate.Set(
"president",
new Dictionary<string, string>
{
["email"] = "lisa@example.com",
["user_name"] = "lisa_m",
}
)
.Set(
"vice_president",
new Dictionary<string, string>
{
["email"] = "tanya@example.com",
["user_name"] = "tanya_o",
}
);
await table.UpdateOneAsync(filter, update);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"updateOne": {
"filter": {
"title": "Chemistry Club"
},
"update": {
"$set": {
"president": {
"email": "lisa@example.com",
"user_name": "lisa_m"
},
"vice_president": {
"email": "tanya@example.com",
"user_name": "tanya_o"
}
}
}
}
}'
Client reference
-
Python
-
TypeScript
-
Go
-
Java
-
C#
-
curl
For more information, see the client reference.
For more information, see the client reference.
|
The Go client is in preview. For more information, see astra-db-go. |
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.
Client reference documentation is not applicable for HTTP.