Search code examples
gremlintinkerpoptinkerpop3

How to search multiple values for same propertykey in gremlin


What is the best way to get values with same property key?

EDIT: Sorry for changing the question my requirement was to get an employee from either of the departments

I need to fetch all the the employees who work for IT or Sales departments and are being managed by manager with id 123.

I have used

g.V().has('managerId',123).out('manages').as('employee')
   .out('worksFor').has('departmentName','IT','Sales')
   .select('employee')

where out('worksAt') gives department.

Can we do this in a has() step or should we use union() step like

g.V().has('managerId',123).out('manages').as('employee').out('worksFor')
    .union(__.has('departmentName','IT'),__.has('departmentName','Sales')
    .select('employee')

Solution

  • You are probably only missing the within predicate which is also explained in the context of the has step in the TinkerPop documentation:

    g.V().has('managerId',123).out('manages').as('employee').out('worksFor').
        has('departmentName',within('IT','Sales')).select('employee')
    

    edit: After reading stephen's answer I noticed that I read over the and in your question:

    employees who work for IT and Sales

    That makes my answer of course invalid. I still leave it here just in case that you actually meant or as indicated by your later use of the union step.