Using the modern graph, how can I write a query that finds the name of the oldest person?

I want to take the graph produced by TinkerFactory.createModern() and write a query that finds the oldest person and returns their name. I want to learn to use the max() traversal step.
4 Replies
spmallette
spmallette•2y ago
I'm not sure I'd use max() in that case. It would be more like:
gremlin> g.V().order().by('age',desc).limit(1).valueMap()
==>[name:[peter],age:[35]]
gremlin> g.V().order().by('age',desc).limit(1).valueMap()
==>[name:[peter],age:[35]]
i guess you might have a case for max() if it was more a traversal like:
gremlin> g.V().values('age').max()
==>35
gremlin> g.V().values('age').max()
==>35
danielcraig23
danielcraig23•2y ago
OK thank you!
kelvinl2816
kelvinl2816•2y ago
For this specific case you would typically use an order...by approach. The tricky part with max is that it is a reducing barrier step, once you use it all traversers that had been exploring the graph are flattened into one. So in this specifc case you typically would not use max but would do something like
gremlin> g.V().has('age').order().by('age',desc).limit(1).valueMap()
==>[name:[peter],age:[35]]
gremlin> g.V().has('age').order().by('age',desc).limit(1).valueMap()
==>[name:[peter],age:[35]]
If you really wanted to use max you need to do something like this
gremlin> g.V().values('age').max().as('m').V().where(eq('m')).by('age').by().valueMap()
==>[name:[peter],age:[35]]
gremlin> g.V().values('age').max().as('m').V().where(eq('m')).by('age').by().valueMap()
==>[name:[peter],age:[35]]
For some reason Discord did not show me Stephen's answers until just now - sorry for duplicate answer! I guess it's good we both said the same thing 🙂
danielcraig23
danielcraig23•2y ago
Thanks Kelvin and Stephen!
Want results from more Discord servers?
Add your server