Arrays
Array Support
NoQL supports many methods that perform operations on array fields. They can be used as part of select statements and queries.
NoQL uses sub-selects with a FROM array field to query array fields in collections. E.g.
Using sub-selects to query array fields
Using '$$ROOT' in sub select promotes the field to the root value of the array
Using '$$ROOT' in sub select
Slicing the array is supported by limit and offset in queries
Slicing an array with limit and offset
Sorting Arrays is supported in MongoDB 5.2+ and NoQL
Sorting Arrays is supported
Aggregation functions are not supported in a sub select
Aggregation functions are not supported in a sub select. For example, the following won't work
sql
--Wont'Work
SELECT id,
(SELECT count(*) AS count FROM Rentals) AS totalRentals
FROM customers
UNWIND Function
UNWIND(array_expr)
NoQL has a high level unwind function that will unwind array fields. For Joins, the unwind join hint should be used.
Complex UNWIND
Supported Array Functions
ALL_ELEMENTS_TRUE
ALL_ELEMENTS_TRUE(array expr)
Returns true when all elements in the array are true.
Example ALL_ELEMENTS_TRUE
usage
ANY_ELEMENT_TRUE
ANY_ELEMENT_TRUE(array expr)
Returns true when any element in the array is true.
Example ANY_ELEMENT_TRUE
usage
ARRAY_ELEM_AT
ARRAY_ELEM_AT(array expr,position)
Returns the element of an array at a position.
ARRAY_RANGE
ARRAY_RANGE(start,stop,step)
Generates an array of numbers from to with the specified step.
ARRAY_TO_OBJECT
ARRAY_TO_OBJECT(array expr)
Converts the array to an object.
Example ARRAY_TO_OBJECT
usage
CONCAT_ARRAYS
CONCAT_ARRAYS(array expr,...)
Concatenate the provided list of arrays.
Example CONCAT_ARRAYS
usage
FIRST_IN_ARRAY
FIRST_IN_ARRAY(array expr)
Returns the first element of an array.
INDEXOF_ARRAY
INDEXOF_ARRAY(array expr,value,[start],[end])
Returns the index of the value in the array.
Example INDEXOF_ARRAY
usage
IS_ARRAY
IS_ARRAY(array expr)
Returns true when the field is an array.
Example IS_ARRAY
usage
LAST_IN_ARRAY
LAST_IN_ARRAY(array expr)
Returns the last element of an array.
OBJECT_TO_ARRAY
OBJECT_TO_ARRAY(expr)
Converts the object to an array.
REVERSE_ARRAY
REVERSE_ARRAY(array expr)
Reverses the order of an array field.
SET_DIFFERENCE
SET_DIFFERENCE(array expr,...)
Returns an array as the difference of the provided arrays.
Example SET_DIFFERENCE
usage
SET_EQUALS
SET_EQUALS(array expr,...)
Returns true or false if the arrays are equal.
Example SET_EQUALS
usage
SET_INTERSECTION
SET_INTERSECTION(array expr,...)
Returns an array as the difference of the provided arrays.
Example SET_INTERSECTION
usage
SET_IS_SUBSET
SET_IS_SUBSET(array expr,...)
Returns whether an array is a subset of another.
Example SET_IS_SUBSET
usage
SET_UNION
SET_UNION(array expr,...)
Returns an array as the union of the provided arrays.
Example SET_UNION
usage
SIZE_OF_ARRAY
SIZE_OF_ARRAY(array expr)
Returns the size of array.
SUM_ARRAY
SUM_ARRAY(array expr,[field])
Sums the values in an array given an array field or sub-select and the field to sum.
Example SUM_ARRAY
usage with a sub select
AVG_ARRAY
AVG_ARRAY(array expr,[field])
Averages the values in an array given an array field or sub-select and the field to average.
ZIP_ARRAY
ZIP_ARRAY(array expr,...)
Transposes an array of input arrays so that the first element of the output array would be an array containing, the first element of the first input array, the first element of the second input array, etc.