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]
ZIP(arr1, arr2)
Pairs by default. When there are exactly two arrays and every value in the first is a string, returns an object (AQL form).
Zips two arrays into an array of pairs.
RETURN ZIP([1, 2], ["a", "b"]) --[[1, "a"], [2, "b"]]
ZIP_OBJECT(keys, values)
Build an object from parallel key and value arrays (AQL-style ZIP of string keys).
RETURN ZIP_OBJECT(["a", "b"], [1, 2]) --{a:1,b:2}
Search Functions
Higher-order functions
Lambdas are x -> expr or (a, b) -> expr. They work as MAP(arr, x -> …) or piped: arr |> MAP(x -> …).
MAP(arr, x -> expr)
Transform each element.
RETURN MAP([1, 2, 3], x -> x * 2)
FILTER(arr, x -> pred)
Keep elements where the predicate is true. Distinct from the FILTER clause.
RETURN FILTER([1, 2, 3, 4], x -> x > 2)
FLAT_MAP(arr, x -> expr)
Map, then flatten one level of nested arrays.
RETURN FLAT_MAP([[1, 2], [3]], x -> x)
GROUP_BY(arr, x -> key)
Returns [{key, items}, …]. Query-level grouping stays COLLECT.
RETURN GROUP_BY(docs, x -> x.city)
SORT_BY(arr, x -> key)
Sort an array by a computed key.
RETURN SORT_BY(docs, x -> x.score)
WINDOW_BY(arr, part?, order)
Adds row_number per partition. Query analytics stay RANK() OVER (…).
RETURN WINDOW_BY(rows, x -> x.k, x -> x.ts)
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))