简体   繁体   中英

using collect in arangodb insert to create new documents

I have a collection called prodSampleNew with documents that have hierarchy levels as fields in arangodb:

 { 
    prodId: 1,   
    LevelOne: "clothes", 
    LevelTwo: "pants", 
    LevelThree: "jeans",
    ... etc.... 
}

I want take the hierarchy levels and convert them into their own documents, so I can eventually build a proper graph with the hierarchy.

I was able to get this to extract the first level fo the hierarchy and put it in a new collection using the following:

for i IN [1]
let HierarchyList = (   
   For prod in prodSampleNew
    COLLECT LevelOneUnique = prod.LevelOne
    RETURN LevelOneUnique
)
FOR hierarchyLevel in HierarchyList
    INSERT {"name":  hierarchyLevel}
    IN tmp

However, having to put a for I IN [1] at the top seems wrong and that there should be a better way.(yes I am fairly new to AQL)

Any pointers on a better way to do this would be appreciated

Not sure what you are trying to achieve exactly. The FOR i IN [1] seems unnecessary however, so you could start your AQL query directly with the subquery to compute the distinct values from hierarchy level 1:

LET HierarchyList = (   
  FOR prod IN prodSampleNew
  COLLECT LevelOneUnique = prod.LevelOne
  RETURN LevelOneUnique
)
FOR hierarchyLevel IN HierarchyList
  INSERT {"name":  hierarchyLevel} IN tmp

The result should be the same.

If the question is more like "how can I get all distinct names of levels from all hierarchies", then you could use something like

LET HierarchyList = UNIQUE(FLATTEN(
  FOR prod IN prodSampleNew 
    RETURN [ prod.LevelOne, prod.LevelTwo, prod.LevelThree ]
))
...

to produce an array with the unique names of the hierarchy levels for level 1-3.

Shouldn't this answer your question, please describe the desired result the query should produce.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM