Gremlin Cheatsheet

# **Gremlin Complete Cheatsheet**

# Basic Traversals
g.V()                                # Get all vertices
g.E()                                # Get all edges
g.V('vertexId')                      # Find a vertex by ID
g.E('edgeId')                        # Find an edge by ID

# Filtering
g.V().hasLabel('person')             # Find vertices by label
g.V().has('name', 'Alice')           # Find vertices with a specific property
g.V().has('age', gt(30))             # Find vertices where age > 30
g.V().has('age', inside(25, 40))     # Find vertices where 25 < age < 40

# Adding Elements
g.addV('person').property('name', 'Alice')    # Add a vertex
g.V().addE('knows').to(g.V('vertexId'))       # Add an edge between vertices

# Updating Properties
g.V('vertexId').property('name', 'Bob')       # Update a property
g.E('edgeId').property('weight', 2.5)         # Update edge property

# Removing Elements
g.V('vertexId').drop()                        # Delete a vertex
g.E('edgeId').drop()                          # Delete an edge

# Traversals
g.V('vertexId').out('knows')                  # Outgoing edges with label 'knows'
g.V('vertexId').in('created')                 # Incoming edges with label 'created'
g.V().out().values('name')                    # Names of connected vertices

# Counting
g.V().count()                                 # Count vertices
g.E().count()                                 # Count edges

# Path Traversals
g.V('vertexId').out().path()                  # Path of connected vertices
g.V('vertexId').repeat(out()).times(3).path() # Recursive path traversal

# Looping
g.V('vertexId').repeat(out()).until(has('name', 'Target')).path()  # Until a condition

# Grouping and Aggregation
g.V().group().by('age').by(count())           # Group vertices by age
g.V().values('age').mean()                    # Average age
g.V().groupCount().by('name')                 # Count vertices by name

# Ordering and Pagination
g.V().order().by('name', asc)                 # Order by name (ascending)
g.V().range(0, 5)                             # First 5 vertices

# Subgraph
g.E().has('weight', gt(0.5)).subgraph('sg').cap('sg')  # Extract a subgraph

# Properties and Metadata
g.V('vertexId').properties()                  # Get properties of a vertex
g.E('edgeId').properties()                    # Get properties of an edge
g.V().valueMap(true)                          # Get all properties with IDs

# Shortest Path (using repeat)
g.V('startId').repeat(out()).until(hasId('endId')).path()

# Complex Conditions
g.V().has('person', 'name', within('Alice', 'Bob'))  # Filter within a set
g.V().or(has('age', gt(50)), has('country', 'USA'))  # Logical OR

# Vertex and Edge Labels
g.V().label()                               # Get labels of vertices
g.E().label()                               # Get labels of edges

# Edge Traversals
g.E().outV()                                # Get the outgoing vertex of an edge
g.E().inV()                                 # Get the incoming vertex of an edge
g.E().bothV()                               # Get both vertices of an edge

# Metrics and Profiling
g.V().profile()                             # Analyze traversal performance
g.V().explain()                             # Explain the traversal steps

# Text Searches
g.V().has('name', textContains('Al'))       # Partial text search
g.V().has('name', textContainsPrefix('Al')) # Prefix text search