Search code examples
gremlinazure-cosmosdb-gremlinapi

How to return adjacent vertices as a property in Gremlin


Say I have a graph that has vertices of labels 'Company' 'CarModel' and 'Parts' where a company has many car models and Car models have many parts. How can I query the Database to return to me all the properties of the Company with 'CarModels' as property that would be an array of the CarModel vertices then again return all the properties of CarModel with Parts as an additional property that has all the properties of Parts?


Solution

  • You can use project step if you want to structure your answer and collect the required data from the graph for each key:

    g.V().hasLabel('Company').
      has('name', <Company Name>).
      project('CompanyData', 'CarModels').
        by(valueMap()).
        by(out().
          project('CarModelData', 'Parts').
            by(valueMap()).
            by(out().valueMap().fold()).fold())
    
    

    example: https://gremlify.com/md1j1rzgigt