Search code examples
azure-cosmosdbgremlingremlin-serverazure-cosmosdb-gremlinapi

Gremlin : Combine 2 or more vertices without an edge between them in Cosmos DB Gremlin API


Suppose I have a vertex Employee and a vertex Department. Employee has a property departmentId but there is no edge between these 2 vertices, can I project departmentName along with employeeName??

g.addV('employee').
  property('id', 1).
  property('name', 'A').
  property('departmentId', 1)

g.addV('department').
  property('id', 1).
  property('name', 'HR')

Solution

  • I still think this is a bad design, and the performance here will be bad.

    g.V().hasLabel('employee').as('e').
      project('name', 'department name').
        by('name').
        by(V().hasLabel('department').
          has('_id', select('e').
            values('departmentId')).values('name'))
    

    example: https://gremlify.com/kudcz61i5j

    Maybe this will have better performance:

    g.V().hasLabel('department', 'employee').
      group().by(coalesce(
          hasLabel('department').values('_id'),
          hasLabel('employee').values('departmentId')
        )).
        by(fold().as('group').unfold().
          hasLabel('employee').
          project('name', 'department name').
            by('name').
            by(select('group').unfold().
              hasLabel('department').values('name')).
          fold())
    

    example: https://gremlify.com/nndmumlshmo