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.
- Typed
-
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); } } - Untyped
-
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); } }
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
-
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); } } - Untyped
-
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); } }
Combine operators
You can use multiple update operators in a single update.
- Typed
-
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); } } - Untyped
-
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); } }
Unsupported operators
-
$renameisn’t supported. Instead, usealterTableto add and drop columns. -
$currentDateisn’t supported. -
The array operators
$push,$each,$pullAll,$addToSet,$pop, and$positionaren’t supported.