kuza
kuza

Reputation: 3041

AQL how to collect documents into arrays under their collection name?

In answer to previous questions was shown how to collect documents under their collection names but there was a clear constraint that query returns only one document for each collection.

@CoDEmanX asked what if the query returns many documents of the same collection?

Upvotes: 2

Views: 460

Answers (1)

kuza
kuza

Reputation: 3041

Will have to rework query to use aggregation:

FOR doc IN ANY "vertex/key" edge_collection
COLLECT collection = PARSE_IDENTIFIER(doc).collection INTO collected
RETURN MERGE({
    [collection]: collected[*].doc
})
  • Group documents by their collection name COLLECT collection = PARSE_IDENTIFIER(doc).collection INTO collected
  • Form document with collection name as property and array of collected documents as value { [collection]: collected[*].doc }
  • Merge results into single document

Upvotes: 3

Related Questions