---
description:
globs:
alwaysApply: true
---
# Gremlin Groovy Cheatsheet
## Rules
- All node and edge labels are case-sensitive
- Our Gremlin environment is based on Gremlin-Groovy. For current timestamps, use new Date()
## Non-Obvious DSL Steps
### coalesce()
Returns the first non-null value from a list of traversals.
```gremlin
g.V().coalesce(values('name'), constant('Unknown'))
```
### choose()
Conditional branching based on a predicate.
```gremlin
g.V().choose(has('age', gt(30)), out('knows'), in('knows'))
```
### repeat()
Loops a traversal a specified number of times or until a condition is met.
```gremlin
g.V().repeat(out()).times(2)
```
### until()
Used with `repeat()` to specify the loop condition.
```gremlin
g.V().repeat(out()).until(has('name', 'marko'))
```
### emit()
Used with `repeat()` to control when to emit results.
```gremlin
g.V().repeat(out()).emit().times(2)
```
### barrier()
Controls the flow of traversers through the traversal.
```gremlin
g.V().barrier().out()
```
### sack()
Manages a side-effect value during the traversal.
```gremlin
g.withSack(0).V().repeat(out().sack(sum, 'weight')).times(2)
```
### aggregate()
Collects values into a list as a side-effect.
```gremlin
g.V().aggregate('x').by('name')
```
### group()
Groups values based on a key.
```gremlin
g.V().group().by(label).by(count())
```
### project()
Projects properties into a map.
```gremlin
g.V().project('name', 'age').by('name').by('age')
```
## Built-In Functions
### id()
Returns the ID of an element.
```gremlin
g.V().id()
```
### label()
Returns the label of an element.
```gremlin
g.V().label()
```
### values()
Returns the values of specified properties.
```gremlin
g.V().values('name')
```
### valueMap()
Returns a map of property keys and values.
```gremlin
g.V().valueMap()
```
### has()
Filters elements based on properties.
```gremlin
g.V().has('name', 'marko')
```
### hasLabel()
Filters elements based on labels.
```gremlin
g.V().hasLabel('person')
```
### out()
Traverses outgoing edges.
```gremlin
g.V().out()
```
### in()
Traverses incoming edges.
```gremlin
g.V().in()
```
### both()
Traverses both incoming and outgoing edges.
```gremlin
g.V().both()
```
### count()
Counts the number of elements.
```gremlin
g.V().count()
```
## Date-Time Functions
### datetime()
Creates a datetime object from a string or epoch milliseconds.
```gremlin
datetime('2023-01-01T00:00:00Z')
```
### plus()
Adds a duration to a datetime.
```gremlin
datetime('2023-01-01T00:00:00Z').plus(duration('P1D'))
```
### minus()
Subtracts a duration from a datetime.
```gremlin
datetime('2023-01-01T00:00:00Z').minus(duration('P1D'))
```
### isBefore()
Checks if one datetime is before another.
```gremlin
datetime('2023-01-01T00:00:00Z').isBefore(datetime('2023-01-02T00:00:00Z'))
```
### isAfter()
Checks if one datetime is after another.
```gremlin
datetime('2023-01-02T00:00:00Z').isAfter(datetime('2023-01-01T00:00:00Z'))
```
### isEqual()
Checks if two datetimes are equal.
```gremlin
datetime('2023-01-01T00:00:00Z').isEqual(datetime('2023-01-01T00:00:00Z'))
```
### format()
Formats a datetime as a string.
```gremlin
datetime('2023-01-01T00:00:00Z').format('yyyy-MM-dd')
```
### parse()
Parses a string into a datetime.
```gremlin
datetime.parse('2023-01-01', 'yyyy-MM-dd')
```
### duration()
Creates a duration object.
```gremlin
duration('P1DT2H')
```
**Note**: All examples in this cheatsheet are compatible with the Gremlin Server HTTP submit endpoint and do not rely on Groovy-specific features. For a complete reference, consult the [Apache TinkerPop documentation](mdc:https:/tinkerpop.apache.org/docs/current/reference).