SurrealDB Docs Logo

Enter a search query

.upsert()

The UPSERT statement can be used to insert records into the database, or to update them if they exist.

Method Syntax
db.upsert(thing, data)
Note

This function replaces the current document / record data with the specified data.

Arguments

ArgumentsDescription
thing required

The table name or the specific RecordID to upsert.

data optional

The document / record data to upsert.

Example usage

# Update all records in a table db.upsert('person', {name: "Jaime"}) # Update a record with a specific ID db.upsert(RecordID('person', 'tobie'), { "name": 'Tobie', "settings": { "active": True, "marketing": True, } })

Translated query

This function will run the following query in the database.

UPSERT $thing CONTENT $data;