Update​

update – update objects in a database

  1. [ with with-item [, ...] ]
  2. update selector-expr
  3. [ filter filter-expr ]
  4. set shape ;

update changes the values of the specified links in all objects selected by update-selector-expr and, optionally, filtered by filter-expr.

with

Alias declarations.

The with clause allows specifying module aliases as well as expression aliases that can be referenced by the update statement. See With block for more information.

update selector-expr

An arbitrary expression returning a set of objects to be updated.

filter filter-expr

An expression of type bool used to filter the set of updated objects.

filter-expr is an expression that has a result of type bool. Only objects that satisfy the filter expression will be updated. See the description of the filter clause of the select statement for more information.

set shape

A shape expression with the new values for the links of the updated object. There are three possible assignment operations permitted within the set shape:

  1. set { field := update-expr [, ...] }
  2. set { field += update-expr [, ...] }
  3. set { field -= update-expr [, ...] }

The most basic assignment is the :=, which just sets the field to the specified update-expr. The += and -= either add or remove the set of values specified by the update-expr from the current value of the field.

Output​

On successful completion, an update statement returns the set of updated objects.

Examples​

Here are a couple of examples of the update statement with simple assignments using :=:

  1. # update the user with the name 'Alice Smith'
  2. with module example
  3. update User
  4. filter .name = 'Alice Smith'
  5. set {
  6. name := 'Alice J. Smith'
  7. };
  8. # update all users whose name is 'Bob'
  9. with module example
  10. update User
  11. filter .name like 'Bob%'
  12. set {
  13. name := User.name ++ '*'
  14. };

For usage of += and -= consider the following Post type:

  1. # ... Assume some User type is already defined
  2. type Post {
  3. required property title -> str;
  4. required property body -> str;
  5. # A "tags" property containing a set of strings
  6. multi property tags -> str;
  7. link author -> User;
  8. }

The following queries add or remove tags from some user’s posts:

  1. with module example
  2. update Post
  3. filter .author.name = 'Alice Smith'
  4. set {
  5. # add tags
  6. tags += {'example', 'edgeql'}
  7. };
  8. with module example
  9. update Post
  10. filter .author.name = 'Alice Smith'
  11. set {
  12. # remove a tag, if it exist
  13. tags -= 'todo'
  14. };

The statement for <x> in <expr> allows to express certain bulk updates more clearly. See Usage of for statement for more details.

See also

EdgeQL > Update

Cheatsheets > Updating data