Oihana PHP Arango

aqlCollect.php

Table of Contents

Functions

aqlCollect()  : string
Builds an AQL `COLLECT` clause for grouping, aggregation, and counting.

Functions

aqlCollect()

Builds an AQL `COLLECT` clause for grouping, aggregation, and counting.

aqlCollect([array<string|int, mixed> $init = [] ]) : string

Supported $init keys

Key Type Description
AQL::ASSIGN array Grouping variables, e.g., ['group' => 'doc.type'].
AQL::AGGREGATE array Aggregation expressions, e.g., ['total' => 'SUM(doc.value)'].
AQL::INTO string Variable name to collect documents into (e.g., 'groupDocs').
AQL::PROJECTION string Projection expression for the INTO clause (e.g., 'doc.name').
AQL::KEEP array An array of variable names to keep (e.g., ['var1', 'var2']).
AQL::WITH_COUNT string Variable name for the count (e.g., AQL::LENGTH).
AQL::OPTIONS array COLLECT options (e.g., ['method' => 'sorted']).

Note: AQL::AGGREGATE and AQL::WITH_COUNT are mutually exclusive in AQL. When both are supplied, AGGREGATE takes precedence and WITH COUNT INTO is dropped. To count alongside other aggregates, express the count as an aggregate (e.g., ['n' => 'LENGTH(1)']).

Examples

Simple Count (for countVertices):

echo aqlCollect([ AQL::WITH_COUNT => AQL::LENGTH ]);
// COLLECT WITH COUNT INTO length

Grouping and Aggregating:

echo aqlCollect
([
    AQL::ASSIGN    => ['type' => 'doc.type'],
    AQL::AGGREGATE => ['count' => 'LENGTH(1)']
]);
// COLLECT type = doc.type AGGREGATE count = LENGTH(1)

Grouping with INTO:

echo aqlCollect
([
    AQL::ASSIGN     => ['type' => 'doc.type'],
    AQL::INTO       => 'items',
    AQL::PROJECTION => '{ name: doc.name, age: doc.age }'
]);
// COLLECT type = doc.type INTO items = { name: doc.name, age: doc.age }
Parameters
$init : array<string|int, mixed> = []

Associative array of collect options.

Tags
throws
ReflectionException
since
1.0.0
author

Marc Alcaraz

Return values
string

The compiled AQL COLLECT clause, or an empty string if invalid.

On this page

Search results