2

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?

kuza
  • 2,761
  • 3
  • 22
  • 56

1 Answers1

3

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
kuza
  • 2,761
  • 3
  • 22
  • 56
  • Why not use `"collection": PARSE_IDENTIFIER(v).collection` and `RETURN { [collection]: groups[*].i.item }`? Seems unnecessary to me create an array for the collection name only to get the sole element back from it. – CodeManX Feb 27 '18 at 15:49
  • Thanks, @CoDEmanX for noting unnecessary overcomplicating. I shall not post QAs after midnight while being totally exhausted. I’ve refactored answer to be as short and concise as possible. – kuza Feb 28 '18 at 08:41