It's always best to provide a Gremlin script that generates some sample data as it makes the context of the question much more clear:
g.addV('u').as('u').
addV('v').property('name','name1').as('v1').
addV('v').property('name','name2').as('v2').
addV('v').property('name','name1').as('v3').
addE('label1').from('u').to('v1').
addE('label2').from('u').to('v1').
addE('label1').from('u').to('v2').
addE('label2').from('u').to('v2').
addE('label2').from('u').to('v3').iterate()
In this case, I would assume that you only want to get the vertex I labelled above as "v1" as it's the only one with two edges (one labelled "label1" and one labelled "label2") AND that has the property key "name1". I'm further assuming that the schema of your graph will only allow for a singular multiplicity of the edges such that you will either have zero or one edges of "label1" and "label2" between "u" and "v*" vertices.
I took this approach, though there may be others:
gremlin> g.V().hasLabel('u').
......1> outE('label1','label2').
......2> where(otherV().has('name','name1')).
......3> groupCount().
......4> by(otherV()).
......5> unfold().
......6> where(select(values).is(eq(2))).
......7> select(keys).
......8> unfold().
......9> valueMap(true)
==>[id:1,name:[name1],label:v]
The main point here is to consider the groupCount()
which is basically giving you the number of edges connecting to otherV()
. You want those to equal 2 in order for the filter to succeed. So, after the groupCount()
, the resulting Map
containing otherV()
for the key and the count of edges for the value is unfolded to entries and filtered at line 6. Once we have that we can just grab the keys from the Map
because we don't need the counts anymore.
I also tried match()
which I don't think is supported by CosmosDB but actually reads pretty nicely in this case:
gremlin> g.V().hasLabel('u').
......1> match(__.as('u').out('label1').as('v'),
......2> __.as('u').out('label2').as('v'),
......3> __.as('v').has('name','name1')).
......4> select('v').
......5> by(valueMap(true))
==>[id:1,name:[name1],label:v]