Update operators for tables (C#)
Some Data API commands, like Update a row (C#), 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:
-
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();
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.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();
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.Set("rating", 4.5)
.Set("genres", new[] { "Fiction", "Drama" });
await table.UpdateOneAsync(filter, update);
}
}
Example setting a map column:
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);
}
}
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.
-
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();
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);
}
}
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:
-
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);
}
}
Example appending to a 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; }
[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);
}
}
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:
-
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);
}
}
Example appending to a 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; }
[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);
}
}
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:
-
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);
}
}
Example removing matches from 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.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);
}
}
Combine operators
You can use multiple update operators in a single 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();
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);
}
}
Unsupported operators
-
$renameisn’t supported. Instead, usealterTableto add and drop columns. -
$currentDateisn’t supported. -
The array operators
$addToSet,$pop, and$positionaren’t supported.