SurrealDB Docs Logo

Enter a search query

.Patch[T](what, patches)

Applies JSON Patch changes to all records, or a specific record, in the database.

Method Syntax
db.Patch[T](what, patches)
Note

This function patches document / record data with the specified JSON Patch data.

Arguments

ArgumentsDescription
thing required

The table name or the specific RecordId to patch.

data optional

The JSON Patch data with which to patch the records.

Example usage

type PatchData struct { Op string `json:"op"` // Operation type: "add", "remove", "replace", etc. Path string `json:"path"` // Path to the field to modify Value interface{} `json:"value"` // New value for the field (only for "add" or "replace") } // Define the patch operations patches := []PatchData{ {Op: "replace", Path: "/name", Value: "John Smith"}, {Op: "add", Path: "/tags", Value: []string{"developer", "engineer"}}, {Op: "remove", Path: "/oldField"}, } // Specify the target record recordID := models.NewRecordID("person", "tobie") // Perform the patch operation updatedPatches, err := surrealdb.Patch(db, recordID, patches) if err != nil { panic(err) } fmt.Printf("Patched person record with patches: %+v\n", updatedPatches)

On this page

© SurrealDB GitHub Discord Community Cloud Features Releases Install