OrientDB has a powerful way to extract parts of a Document field. This applies to the Java API, SQL Where conditions, and SQL projections.
To extract parts you have to use the square brackets.
Example: tags is an EMBEDDEDSET of Strings containing the values ['Smart', 'Geek', 'Cool'].
The expression tags will return 'Smart'.
Inside square brackets put the items separated by comma ",".
Following the tags example above, the expression tags[0,2] will return a list with [Smart, 'Cool'].
Inside square brackets put the lower and upper bounds of an item, separated by "-".
Following the tags example above, the expression tags[1-2] returns ['Geek', 'Cool'].
SELECT * FROM profile WHERE phones['home'] LIKE '+39%'
Works the same with double quotes.
You can go in a chain (contacts is a map of map):
SELECT * FROM profile WHERE contacts[phones][home] LIKE '+39%'
With lists and arrays you can pick an item element from a range:
SELECT * FROM profile WHERE tags = 'smart'
and single items:
SELECT * FROM profile WHERE tags[0,3,5] CONTAINSALL ['smart', 'new', 'crazy']
and a range of items:
SELECT * FROM profile WHERE tags[0-5] CONTAINSALL ['smart', 'new', 'crazy']
Inside the square brackets you can specify a condition. Any SQL condition is supported.
employees[label = 'Ferrari'] employees[age > 25 AND name = 'John']
You can cross a graph using a projection. This an example of traversing all the retrieved nodes with name "Tom". "out" is outEdges and it's a collection. Previously, a collection couldn't be traversed with the . notation. Example:
SELECT out.in FROM v WHERE name = 'Tom'
This retrieves all the vertices connected to the outgoing edges from the Vertex with name = 'Tom'.
A collection can be filtered with the equals operator. This an example of traversing all the retrieved nodes with name "Tom". The traversal crosses the out edges but only where the linked (in) Vertex has the label "Ferrari" and then forward to the:
SELECT out[in.label = 'Ferrari'] FROM v WHERE name = 'Tom'
Or selecting vertex nodes based on class:
SELECT out[in[email protected]class = 'Car'] FROM v WHERE name = 'Tom'
SELECT out[label='drives'][in[email protected]class = 'Car'] FROM v WHERE name = 'Tom'
As you can see where brackets () follow brackets, the result set is filtered in each step like a Pipeline.
NOTE: This doesn't replace the support of GREMLIN. GREMLIN is much more powerful because it does thousands of things more, but it's a simple and, at the same time, powerful tool to traverse relationships.