package update

import "github.com/datastax/astra-db-go/v2/astra/update"

Package update provides helpers for constructing update documents for collection and table updates.

The update operators supported for collections and tables differ, so be sure to check the docs for which operators are supported for each resource type:

Index

Types

type CollectionUpdate

type CollectionUpdate interface {
	// contains filtered or unexported methods
}

CollectionUpdate is implemented by types that can be used as an update document for collection operations. It is satisfied by CollectionUpdateBuilder and U.

type CollectionUpdateBuilder

type CollectionUpdateBuilder struct {
	// contains filtered or unexported fields
}

CollectionUpdateBuilder accumulates Collection Update Operators and serializes directly to JSON.

Construct with Coll():

update.Coll().Set("number_of_pages", 423).Unset("rating").Inc("copies_sold", 1)
func Coll
func Coll() *CollectionUpdateBuilder

Coll returns an empty CollectionUpdateBuilder. Chain methods like [Set], [Unset], etc. to add operators and fields.

Example usage:

update.Coll().Set("number_of_pages", 423).Unset("rating").Inc("copies_sold", 1)
func (*CollectionUpdateBuilder) AddToSet
func (u *CollectionUpdateBuilder) AddToSet(field string, value any) *CollectionUpdateBuilder

The $addToSet operator adds an item to an array only if the item does not already exist in the array.

Example usage:

update.Coll().AddToSet("genres", "SciFi")
func (*CollectionUpdateBuilder) AddToSetEach
func (u *CollectionUpdateBuilder) AddToSetEach(field string, values ...any) *CollectionUpdateBuilder

AddToSetEach combines [AddToSet] and $each to add items to an array if the items do not already exist in the array.

Example usage:

update.Coll().AddToSetEach("genres", "SciFi", "Fantasy")
func (*CollectionUpdateBuilder) CurrentDate
func (b *CollectionUpdateBuilder) CurrentDate(field string) *CollectionUpdateBuilder

The $currentDate operator sets the value of the specified field to the current date and time.

Example usage:

update.Coll().CurrentDate("due_date")
func (*CollectionUpdateBuilder) Inc
func (b *CollectionUpdateBuilder) Inc(field string, amount any) *CollectionUpdateBuilder

The $inc operator increments the value of the specified field by the specified amount.

Example usage:

update.Coll().Inc("number_of_pages", 25)
func (*CollectionUpdateBuilder) MarshalAstraRaw
func (u *CollectionUpdateBuilder) MarshalAstraRaw(ctx serdes.EncodeCtx, dst []byte) ([]byte, error)
func (*CollectionUpdateBuilder) Max
func (b *CollectionUpdateBuilder) Max(field string, value any) *CollectionUpdateBuilder

The $max operator updates the specified field only if the specified value is greater than the existing field value.

Example usage:

update.Coll().Max("rating", 3.9)
func (*CollectionUpdateBuilder) Min
func (b *CollectionUpdateBuilder) Min(field string, value any) *CollectionUpdateBuilder

The $min operator updates the specified field only if the specified value is less than the existing field value.

Example usage:

update.Coll().Min("rating", 3.9)
func (*CollectionUpdateBuilder) Mul
func (b *CollectionUpdateBuilder) Mul(field string, value any) *CollectionUpdateBuilder

The $mul operator multiplies the value of the specified field.

Example usage:

update.Coll().Mul("rating", 1.2)
func (*CollectionUpdateBuilder) Pop
func (u *CollectionUpdateBuilder) Pop(field string, value int) *CollectionUpdateBuilder

The $pop operator removes the first or last item of the array, depending on the value of the operator. Use -1 to remove the first item. Use 1 to remove the last item.

Example usage:

update.Coll().Pop("genres", -1) // Removes the first item in the genres array.
func (*CollectionUpdateBuilder) Push
func (u *CollectionUpdateBuilder) Push(field string, value any) *CollectionUpdateBuilder

The $push operator appends data to the field value. The specified field must be an array or must not yet exist.

If the specified field does not exist, the field is created, and the value is an array containing the pushed items.

Use [PushEach] to append multiple properties. Use [PushEachPosition] to modify the position of the new items in the array.

Example usage:

update.Coll().Push("genres", "SciFi")
func (*CollectionUpdateBuilder) PushEach
func (u *CollectionUpdateBuilder) PushEach(field string, values ...any) *CollectionUpdateBuilder

The $push operator with $each modifier appends multiple values to an array field.

Example usage:

update.Coll().PushEach("genres", "SciFi", "Fantasy")
func (*CollectionUpdateBuilder) PushEachPosition
func (u *CollectionUpdateBuilder) PushEachPosition(field string, position int, values ...any) *CollectionUpdateBuilder

The $position operator modifies the $push operator to specify the position in the array to add items.

When you use position, the $each operator is required, even if you want to insert a single item at the specified position.

Example usage:

update.Coll().PushEachPosition("genres", 1, "SciFi", "Fantasy")
func (*CollectionUpdateBuilder) Rename
func (u *CollectionUpdateBuilder) Rename(field, newName string) *CollectionUpdateBuilder

The $rename operator renames the specified field.

Example usage:

update.Coll().Rename("old_field", "new_field").Rename("other_old_field", "other_new_field")
func (*CollectionUpdateBuilder) Set
func (u *CollectionUpdateBuilder) Set(field string, value any) *CollectionUpdateBuilder

The $set operator sets the value of the specified field to the specified value.

Example usage:

update.Coll().Set("number_of_pages", 423).Set("rating", 4.5)
func (*CollectionUpdateBuilder) SetOnInsert
func (u *CollectionUpdateBuilder) SetOnInsert(field string, value any) *CollectionUpdateBuilder

The $setOnInsert operator sets the value of the specified field only if an upsert is performed.

Example usage:

update.Coll().SetOnInsert("rating", 5.0).SetOnInsert("is_checked_out", false)
func (*CollectionUpdateBuilder) Unset
func (b *CollectionUpdateBuilder) Unset(fields ...string) *CollectionUpdateBuilder

The $unset operator removes the specified field(s).

Example usage:

update.Coll().Unset("borrower", "due_date")

type TableUpdate

type TableUpdate interface {
	// contains filtered or unexported methods
}

TableUpdate is implemented by types that can be used as an update document for table operations. It is satisfied by TableUpdateBuilder and U.

type TableUpdateBuilder

type TableUpdateBuilder struct {
	// contains filtered or unexported fields
}

TableUpdateBuilder accumulates Table Update Operators and serializes directly to JSON.

Construct with Table():

update.Table().Set("name", "Bob").Unset("phone")
func Table
func Table() *TableUpdateBuilder

Table returns an empty TableUpdateBuilder.

func (*TableUpdateBuilder) MarshalAstraRaw
func (u *TableUpdateBuilder) MarshalAstraRaw(ctx serdes.EncodeCtx, dst []byte) ([]byte, error)
func (*TableUpdateBuilder) PullAll
func (u *TableUpdateBuilder) PullAll(field string, values ...any) *TableUpdateBuilder

The $pullAll operator removes the specified elements from a list or set, or removes entries that match the specified keys from a map.

Example usage:

update.Table().PullAll("genres", "SciFi", "Romance")
func (*TableUpdateBuilder) Push
func (u *TableUpdateBuilder) Push(field string, value any) *TableUpdateBuilder

The $push operator appends a single element to a map, list, or set. To append multiple items, use [PushEach].

Example usage:

update.Table().Push("genres", "SciFi")
func (*TableUpdateBuilder) PushEach
func (u *TableUpdateBuilder) PushEach(field string, values ...any) *TableUpdateBuilder

The $push operator with $each modifier appends multiple values to an array field.

Example usage:

update.Table().PushEach("topics", "robots", "AI")
func (*TableUpdateBuilder) Set
func (u *TableUpdateBuilder) Set(field string, value any) *TableUpdateBuilder

The $set operator sets the value of the specified field to the specified value. To update a value to a map that 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.

Example setting a non-map column:

update.Table().Set("name", "Bob").Set("age", 30)

Example setting a map column with string keys (normal map):

update.Table().Set("metadata", update.U{"language": "English", "edition": "First"})

Example setting a map column with non-string keys (array of key-value pairs):

update.Table().Set("map_column_int_str", [][]any{{1, "value1"}, {2, "value2}})
func (*TableUpdateBuilder) Unset
func (u *TableUpdateBuilder) Unset(fields ...string) *TableUpdateBuilder

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.

Example usage:

update.Table().Unset("borrower", "due_date")

type U

type U map[string]any

U represents an update document as a map.

Use operator keys directly:

// update.U{"name": "new"} is equivalent to map[string]any{"name": "new"}.
update.U{"$set": update.U{"name": "Bob", "age": 30}},

You can also chain fluent functions:

// Equivalent to the above example.
update.Coll().Set("name", "Bob").Set("age", 30)