Update operators for tables
Some Data API commands, like Update a row, 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:
-
Python
-
TypeScript
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")
# Update a row
table.update_one(
{"title": "Hidden Shadows of the Past", "author": "John Anthony"},
{
"$set": {"rating": 4.5, "genres": ["Fiction", "Drama"]},
},
)
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_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"],
},
},
);
})();
import com.datastax.astra.client.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.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"));
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("rating")]
public double? Rating { get; set; }
[ColumnName("genres")]
public List<string>? Genres { get; set; }
}
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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.Set(b => b.Rating, 4.5)
.Set(b => b.Genres, new List<string> { "Fiction", "Drama" });
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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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.Set("rating", 4.5)
.Set("genres", new[] { "Fiction", "Drama" });
await table.UpdateOneAsync(filter, update);
}
}
curl -sS -L -X POST "API_ENDPOINT/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"]
}
}
}
}'
Example setting a map column:
-
Python
-
TypeScript
-
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.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.data_types import DataAPIMap
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_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,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_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 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.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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/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"
}
}
}
}
}'
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.
-
Python
-
TypeScript
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")
# Update a row
table.update_one(
{"title": "Hidden Shadows of the Past", "author": "John Anthony"},
{"$unset": {"genres": ""}},
)
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_NAME",
});
// Update a row
(async function () {
await table.updateOne(
{
title: "Hidden Shadows of the Past",
author: "John Anthony",
},
{
$unset: {
genres: "",
},
},
);
})();
import com.datastax.astra.client.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.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 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("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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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/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": ""
}
}
}
}'
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:
-
Python
-
TypeScript
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_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,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_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"],
},
},
},
);
})();
import com.datastax.astra.client.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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/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"]
}
}
}
}
}'
Example appending to a map column:
-
Python
-
TypeScript
-
Java
-
C#
-
curl
When appending to a map, 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.
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.data_types import DataAPIMap
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_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"]]
},
}
},
)
When appending to a map, 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,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_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"]],
},
},
},
);
})();
When appending to a map, 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.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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);
}
}
When appending to a map, 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/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"]
]
}
}
}
}
}'
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:
-
Python
-
TypeScript
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_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,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_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"],
},
},
},
);
})();
import com.datastax.astra.client.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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/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"]
}
}
}
}
}'
Example appending to a map column:
-
Python
-
TypeScript
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.data_types import DataAPIMap
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_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"]]
},
}
},
)
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_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"]],
},
},
},
);
})();
import com.datastax.astra.client.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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);
}
}
curl -sS -L -X POST "API_ENDPOINT/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"]
]
}
}
}
}
}'
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:
-
Python
-
TypeScript
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")
# Update a row
table.update_one(
{"title": "Hidden Shadows of the Past", "author": "John Anthony"},
{
"$pullAll": {
"genres": ["SciFi", "Romance"],
}
},
)
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_NAME",
});
// Update a row
(async function () {
await table.updateOne(
{
title: "Hidden Shadows of the Past",
author: "John Anthony",
},
{
$pullAll: {
genres: ["SciFi", "Romance"],
},
},
);
})();
import com.datastax.astra.client.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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/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"]
}
}
}
}'
Example removing matches from map column:
-
Python
-
TypeScript
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")
# Update a row
table.update_one(
{"title": "Hidden Shadows of the Past", "author": "John Anthony"},
{
"$pullAll": {
"metadata": ["language", "edition"],
}
},
)
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_NAME",
});
// Update a row
(async function () {
await table.updateOne(
{
title: "Hidden Shadows of the Past",
author: "John Anthony",
},
{
$pullAll: {
metadata: ["language", "edition"],
},
},
);
})();
import com.datastax.astra.client.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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/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"]
}
}
}
}'
Combine operators
You can use multiple update operators in a single update.
-
Python
-
TypeScript
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_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,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_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: "",
},
},
);
})();
import com.datastax.astra.client.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.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 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("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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
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/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": ""
}
}
}
}'
Unsupported operators
-
$renameisn’t supported. Instead, usealterTableto add and drop columns. -
$currentDateisn’t supported. -
The array operators
$addToSet,$pop, and$positionaren’t supported.