EEdgedb array usage
[tr] Türkçe Oku 2023-04-11
The array data type is used to store multiple data elements in a single data structure. In edgedb, the ‘array of’ syntax is used to define arrays. For example, the following example defines an array named colors
which contains color names of type str
:
CREATE TYPE Product {
required property name -> str;
required property colors -> array of str;
};
INSERT Product {
name := "T-Shirt",
colors := ["Red", "Green", "Blue"]
};
SELECT Product.colors;
In this example, an object type named Product
is defined with an array property named colors
. The INSERT
statement creates an instance of the Product
object type and assigns an array containing three different color names to the colors
property. The SELECT
statement queries the colors
property of elements in the Product
object type and returns the result
Arrays are a useful data structure in databases. edgedb provides a set of specialized processor functions for processing arrays. These functions can be used to sort elements within an array, search for specific elements or items within an array, filter elements within an array, aggregate elements within an array, and perform many other operations on arrays.
The following examples demonstrate array operations in Edgedb:
SELECT array_agg(User ORDER BY User.name);
SELECT User FILTER User.name IN ["John", "Alice"];
SELECT User FILTER User.name ILIKE "a%";
SELECT SUM(array_agg(Product.price));
These examples only show a few of the many functions that can be used to process arrays. edgedb provides many more functions for processing elements within an array.