Update operators for tables (TypeScript)
Some Data API commands, like Update a row (TypeScript), use update operators to modify rows in a table.
Set
The $set operator sets the value of the specified column to the specified value.
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"),
keyspace: "KEYSPACE_NAME",
});
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
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.
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"),
keyspace: "KEYSPACE_NAME",
});
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: "",
},
},
);
})();
Combine operators
You can use multiple update operators in a single update.
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"),
keyspace: "KEYSPACE_NAME",
});
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: "",
},
},
);
})();
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.