Skip to main content
All CollectionsData AnalysisGremlin training
Gremlin Training 6: Basic filtering steps - hasId()
Gremlin Training 6: Basic filtering steps - hasId()

Learn to use the hasId()-step to filter single or multiple components and references based on their unique ids.

Kristine Marhilevica avatar
Written by Kristine Marhilevica
Updated over a year ago

The hasId()-step can be used to filter components or references based on their id. Ids are unique, so filtering based on id can be useful when there exists multiple components and references which cannot easily be distinguished based on their other properties. In the image to the right, we see that searching for a component with a specific name yields six seemingly identical results. The id of a component cannot be accessed as a property using the has()-step. Instead, the hasId()-step will have to be used. The hasId()-step takes an arbitrary amount of ids as arguments and filters aways all components or references whose id is not within the set of ids passed to the hasId()-step.

hasId()-step

In this first example we will use the hasId()-step with a single id argument to limit our search to a single component with a specific id. The following query limits the search to include only the component with the id "11ec0d0b2e9491e421534f12".

g.V().hasId('11ec0d0b2e9491e421534f12')
id

It is also possible to filter by id when doing the initial selection. The query above is equivalent to the following query:

g.V('11ec0d0b2e9491e421534f12')
id query

Next, we will use the hasId()-step with multiple ids to limit our search to a set of components whose ids are within a set of provided ids. The following query limits the search to include only components whose id is "11ec0d0b2e9491e421534f12", "926a4c754bb2a12f532bbbd2", "b488e8087551f40a184275a1" or "e0d5a6d42a220984a3cabc8a":

g.V().  hasId(    "11ec0d0b2e9491e421534f12",    "926a4c754bb2a12f532bbbd2",    "b488e8087551f40a184275a1",    "e0d5a6d42a220984a3cabc8a")
multiple ids

The above query is equivalent to the follwoing query:

g.V(  "11ec0d0b2e9491e421534f12",  "926a4c754bb2a12f532bbbd2",  "b488e8087551f40a184275a1",  "e0d5a6d42a220984a3cabc8a")
id query

Reference id filtering work similarly:

g.E().  hasId(    'b081d11ff8b201d532533391',    'a36bd6a603b8aca43ab20d77')
reference id filtering

The above query is equivalent to the following query:

g.E('b081d11ff8b201d532533391', 'a36bd6a603b8aca43ab20d77')
equivalent query
Did this answer your question?