Search code examples
surrealdb

How to edit and delete properties of objects within an array on a record


I am having a hard time editing and deleting objects in an array within a record. I can figure out how to add objects to an array:

UPDATE my_table:record_id SET array += {
    id: "id_of_this_object_123456",
    name: "Josh"
    age: 21
}

But i dont know how i would update specific properties in the objects within the array. This is my try in doing so, but this does not work:

UPDATE my_table:record_id.array MERGE {
    age: 25
} WHERE id == "id_of_this_object_123456"

I dont know either how to delete an object from an array. This does not work either:

UPDATE my_table:record_id SET parameter -= * WHERE parameters.id == "id_of_this_object_123456"

How do i manipulate array of objects within a record depentent on some criteria, e.g. id or name?


Solution

  • I found out! These two queries work:

    Update:

    UPDATE record_id SET array[WHERE id == "id_of_this_object_123456"] = { 
        id: "id_of_this_object_123456",
        name: "Josh",
        age: 25,
    }
    

    Delete:

    UPDATE record_id SET array = array[WHERE id != "id_of_this_object_123456"]
    

    For the update solution, i would rather have a CONTENT solution, where i dont have to specify all properties, but i have not found a solution for this yet.