Querying

In this section you will find example queries that show how to perform a range of different tasks on composite databases.

The examples featured in this section make use of the two Cypher clauses: USE and CALL {}.

Connecting drivers and applications

Drivers and client applications connect to composite databases just like standard databases. For more information, see Databases and execution context in the Neo4j Driver manuals.

Graph selection

Queries submitted to a composite database may contain several USE clauses that direct different parts of the query to different constituent graphs.

Each constituent graph is named after the alias that introduces it into the composite database.

The examples will assume the same setup as the one created in the Introduction

Query a single graph

Example 1. Reading and returning data from a single graph
USE cineasts.latest
MATCH (movie:Movie)
RETURN movie.title AS title

The USE clause at the beginning of the query selects the cineasts.latest graph for all the subsequent clauses. MATCH is performed on that graph.

Query multiple graphs

Example 2. Reading and returning data from two graphs
USE cineasts.latest
MATCH (movie:Movie)
RETURN movie.title AS title
  UNION
USE cineasts.upcoming
MATCH (movie:Movie)
RETURN movie.title AS title

The first part of the UNION query selects the cineasts.latest graph and the second part selects the cineasts.upcoming graph.

Dynamic graph access

Queries can also select constituent graphs dynamically, using the form USE graph.byName(graphName).

Example 3. Reading and returning data from dynamically selected graphs
UNWIND ['cineasts.latest', 'cineasts.upcoming'] AS graphName
CALL {
  USE graph.byName(graphName)
  MATCH (movie:Movie)
  RETURN movie
}
RETURN movie.title AS title

In the example above, the part of the query accessing graph data, MATCH (movie:Movie), is wrapped in a sub-query with a dynamic USE clause. UNWIND is used to get the names of our graphs, each on one row. The CALL {} sub-query executes once per input row. In this case, once selecting cineasts.latest, and once selecting cineasts.upcoming.

Listing graphs

The built-in function graph.names() returns a list containing the names of all constituent graphs on the current composite database.

Example 4. The graph.names() function
UNWIND graph.names() AS graphName
RETURN graphName
+---------------------+
| graphName           |
+---------------------+
| "cineasts.latest"   |
| "cineasts.upcoming" |
+---------------------+

The names returned by this function can be used for dynamic graph access.

Example 5. Reading and returning data from all graphs
UNWIND graph.names() AS graphName
CALL {
  USE graph.byName(graphName)
  MATCH (movie:Movie)
  RETURN movie
}
RETURN movie.title

Query result aggregation

Example 6. Getting the earliest release year of all movies from all graphs
UNWIND graph.names() AS graphName
CALL {
  USE graph.byName(graphName)
  MATCH (movie:Movie)
  RETURN movie.released AS released
}
RETURN min(released) AS earliest

The sub-query returns the released property of each movie, from each constituent graph. The RETURN at the end of the main query aggregates across the full result to calculate the global minimum.

Correlated subqueries

This query finds all movies in cineasts.upcoming that are to be released in the same month as the longest movie in cineasts.latest.

Example 7. Correlated subquery
CALL {
  USE cineasts.latest
  MATCH (movie:Movie)
  RETURN movie.releasedMonth AS monthOfLongest
    ORDER BY movie.runningTime DESC
    LIMIT 1
}
CALL {
  USE cineasts.upcoming
  WITH monthOfLongest
  MATCH (movie:Movie)
  WHERE movie.releasedMonth = monthOfLongest
  RETURN movie
}
RETURN movie

The first part of the query finds the movie with the longest running time from cineasts.latest, and returns its release month. The second part queries for all movies in cineasts.upcoming that fulfill our condition and returns them. The sub-query imports the monthOfLongest variable using WITH monthOfLongest, to make it accessible.

Updates

Composite database queries can perform updates to constituent graphs.

Example 8. Constituent graph update
USE cineasts.upcoming
CREATE (:Movie {title: 'Dune: Part Two'})

Updates can only be performed on a single constituent graph per transaction.

Example 9. Multi-graph update will fail
UNWIND graph.names() AS graphName
CALL {
  USE graph.byName(graphName)
  CREATE (:Movie {title: 'The Flash'})
}
Writing to more than one database per transaction is not allowed.

Limitations

Queries on composite databases have a few limitations.

Graph accessing operations

Consider a composite database query:

UNWIND graph.names() AS graphName
CALL {
  USE graph.byName(graphName)
  MATCH (movie:Movie)
  RETURN movie
}
RETURN movie

Here the outer clauses, i.e. the UNWIND, the CALL itself, and the final RETURN, appear in the root scope of the query, without a specific chosen graph. Clauses or expressions in scopes where no graph has been specified must not be graph-accessing.

See examples of graph-accessing operations:

  • MATCH (n)

  • CREATE (:X)

  • WITH [p=()--() | p] AS paths

Examples of non-graph-accessing operations:

  • RETURN 1 + 2 AS number

  • WITH node.property AS val

Nested USE clauses

An inner scope must use the same graph as its outer scope:

USE cineasts.latest
MATCH (n)
CALL {
  USE cineasts.upcoming
  MATCH (m)
  RETURN m
}
RETURN n, m
Nested subqueries must use the same graph as their parent query.
Attempted to access graph cineasts.upcoming
"    USE cineasts.upcoming"
     ^

Sub-queries without a USE clause can be nested. They inherit the specified graph from the outer scope.

CALL {
  USE cineasts.upcoming
  CALL {
    MATCH (m:Movie)
    RETURN m
  }
  RETURN m
}
RETURN m

Built-in graph functions

Graph functions are located in the namespace graph. The following table provides a description these functions:

Table 1. Built-in graph functions
Function Explanation

graph.names()

Provides a list of names of all constituent graphs on the current composite database.

graph.byName(graphName)

Used with the USE clause to select a constituent graph by name dynamically. This function is supported only with USE clauses.