Array Functions
Functions for creating, accessing, transforming, and combining arrays in SDBQL.
Array Operations
[*] Array Spread Operator
The array spread operator [*] extracts a specific field from all elements in an array, returning a new array with just those values. This is similar to a map operation.
-- Extract user_key from all attendees
FOR event IN events
RETURN event.attendees[*].user_key
-- Result: [["u1", "u2"], ["u3"]]
-- Nested field extraction
FOR doc IN data
RETURN doc.items[*].user.name
-- Result: [["Alice", "Bob"]]
Basic Usage
-- Given: {items: [{name: "A"}, {name: "B"}]}
doc.items[*].name -- ["A", "B"]
Chained Spread
-- Given: {items: [{tags: ["a","b"]}, {tags: ["c"]}]}
doc.items[*].tags[*] -- ["a", "b", "c"]
Missing Fields
-- Given: {items: [{name: "A"}, {other: "B"}]}
doc.items[*].name -- ["A", null]
Bare Spread
-- Given: {values: [1, 2, 3]}
doc.values[*] -- [1, 2, 3]
Note:
If the base value is not an array, an empty array is returned.
When a field is missing from an array element, null is included in the result.
Access Functions
Transformation Functions
Combination Functions
UNION(arr1, arr2)
Union of arrays (produces unique values).
RETURN UNION([1, 2], [2, 3]) --[1, 2, 3]
INTERSECTION(arr1, arr2...)
Returns common elements between arrays.
RETURN INTERSECTION([1, 2], [2, 3]) --[2]
Search Functions
Practical Examples
-- Combine and deduplicate tags
LET tags1 = ["rust", "database"]
LET tags2 = ["database", "nosql"]
RETURN UNION(tags1, tags2) -- ["rust", "database", "nosql"]
-- Get top 3 values from sorted array
LET values = [5, 2, 8, 1, 9, 3]
RETURN SLICE(SORTED(values), 3, 3) -- [5, 8, 9] (last 3)
-- Flatten nested arrays
LET nested = [[1, 2], [3, [4, 5]]]
RETURN FLATTEN(nested, 2) -- [1, 2, 3, 4, 5]
-- Find users with specific skills
FOR user IN users
FILTER LENGTH(INTERSECTION(user.skills, ["rust", "go"])) > 0
RETURN user.name
-- Extract all unique tags from documents
LET allTags = (FOR doc IN documents RETURN doc.tags)
RETURN SORTED_UNIQUE(FLATTEN(allTags))