In SurrealDB SQL, How do you find and update value in array of objects

2 min read 30-09-2024
In SurrealDB SQL, How do you find and update value in array of objects


SurrealDB SQL is a powerful database that allows developers to work with data in innovative ways. One common task when working with databases is to find and update specific values within arrays of objects. This article will guide you through the process of finding and updating values in arrays of objects using SurrealDB SQL.

Problem Scenario

Let's say you have the following JSON structure in your SurrealDB database:

INSERT INTO users (id, name, hobbies) VALUES 
    (1, 'John', [{ 'type': 'sports', 'name': 'soccer' }, { 'type': 'music', 'name': 'guitar' }]),
    (2, 'Alice', [{ 'type': 'sports', 'name': 'basketball' }, { 'type': 'music', 'name': 'piano' }]);

Now, you want to find and update John's hobby from 'soccer' to 'football'. The SQL query to achieve this task needs to be clearly defined to effectively access and manipulate the array of objects.

Solution

To update John's hobby in the array of objects, you can use the following SQL statement:

UPDATE users 
SET hobbies = ARRAY_REPLACE(hobbies, 
    (SELECT hobby FROM hobbies WHERE name = 'soccer'), 
    { 'type': 'sports', 'name': 'football' })
WHERE name = 'John';

This query accomplishes the following:

  1. Identify the Record: It uses the WHERE clause to target John’s record specifically.
  2. Locate the Hobby: The sub-query selects the hobby object where the name is 'soccer'.
  3. Update the Hobby: The ARRAY_REPLACE function is used to replace the old hobby object with a new one where the name is 'football'.

Breakdown of the Query Components:

  • UPDATE Statement: This indicates that we are modifying existing records in the users table.
  • SET Clause: This defines what field we want to update (in this case, hobbies).
  • ARRAY_REPLACE: This is a built-in function in SurrealDB that allows you to search for a specific value in an array and replace it with another.
  • WHERE Clause: This condition filters the record we intend to update, ensuring that we only modify John's record.

Practical Example

Consider the following situation where a user wishes to add an additional hobby instead of replacing an existing one. You might want to add 'swimming' to John's hobbies:

UPDATE users 
SET hobbies = ARRAY_APPEND(hobbies, { 'type': 'sports', 'name': 'swimming' }) 
WHERE name = 'John';

In this scenario, the ARRAY_APPEND function is used to add a new object to the existing hobbies array without deleting any of the previous hobbies.

Conclusion

Understanding how to find and update values in an array of objects in SurrealDB SQL is crucial for effective data manipulation. The examples provided demonstrate both the replacement and addition of values, offering a clear approach to managing complex data structures.

Useful Resources

  • SurrealDB Documentation - The official documentation is an excellent resource for learning about different features and functions available in SurrealDB.
  • SQL Functions Reference - A comprehensive reference for all SQL functions, including array manipulation functions.

By leveraging these techniques, developers can work more efficiently with data stored in arrays of objects, enhancing the overall functionality of their applications.