Skip to content

Commit b509968

Browse files
committedJan 27, 2025
Example of startingWith in where
This closes #223
1 parent 9d7237f commit b509968

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed
 

‎book/Section-Writing-Gremlin-Queries.adoc

+19-5
Original file line numberDiff line numberDiff line change
@@ -4960,6 +4960,25 @@ g.V().has('airport','city','London').
49604960
[country:[CA],code:[YXU],region:[CA-ON]]
49614961
----
49624962

4963+
As the 'where' step takes a predicate for its matching conditions, it allows for a
4964+
great deal of flexibilty in the types of filtering that you can do. In the following
4965+
example, we find '"IAD"' and use the first three letters of its city name to find
4966+
other airports that start with those three letters:
4967+
4968+
[source,groovy]
4969+
----
4970+
g.V().has('airport','code','IAD').as('a').
4971+
V().hasLabel('airport').
4972+
where(startingWith('a')).
4973+
by('city').
4974+
by(values('city').substring(0,3)).
4975+
values('city')
4976+
----
4977+
4978+
Note that the order of the 'by' modulators is such that the first 'by' refers to the
4979+
incoming traverser to 'where' and the second refers to the "a" vertex referenced in
4980+
the 'startingWith'.
4981+
49634982
We've seen how the number of 'by' modulators correspond to the parameters in the
49644983
'where' and that they are applied in a round-robin fashion, starting with the
49654984
value of the traverser being compared and then to the values it is to be compared
@@ -5028,11 +5047,6 @@ Nice. Note that this also means that Nice is included in the results. Lastly we
50285047
the part of the query that prepares the output in a form we want in a 'local' step so
50295048
that a separate list is created for each airport.
50305049

5031-
You could write this query other ways, perhaps using a 'match' step but once you
5032-
understand the pattern used above it is both fairly simple and quite powerful.
5033-
5034-
5035-
50365050
[[choose]]
50375051
Using 'choose' to write if...then...else type queries
50385052
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)