Querying

This guide shows how to create a simple Cypher® query to MATCH and RETURN Movies using the Cypher Builder:

MATCH(m:Movie)
RETURN m

Instructions

  1. Define a node variable by creating a new instance of Cypher.Node:

    const movieNode = new Cypher.Node();
  2. Create the pattern to be matched against with Cypher.Pattern:

    const matchPattern = new Cypher.Pattern(movieNode, { labels: ["Movie"] });
  3. Create a MATCH clause and pass the pattern as an argument:

    const clause = new Cypher.Match(matchPattern);
  4. To RETURN the Node variables, use the .return method in the MATCH clause:

    clause.return(movieNode);
    1. Alternatively, you can do the same thing by creating the clause in a single statement:

      const clause = new Cypher.Match(matchPattern).return(movieNode);
  5. After the query is ready, it is time to build it. This step will generate the actual Cypher string that can be used in a Neo4j database:

    const { cypher } = clause.build();
    
    console.log(cypher);

Conclusion

Altogether, your script should look like this:

import Cypher from "@neo4j/cypher-builder";

const movieNode = new Cypher.Node();

const matchPattern = new Cypher.Pattern(movieNode, { labels: ["Movie"] });

const clause = new Cypher.Match(matchPattern).return(movieNode);

const { cypher } = clause.build();

console.log(cypher);

Now, if you execute node main.js again, it will output the following query:

MATCH (this0:Movie)
RETURN this0

Note that this0 is an autogenerated name for the Node variable created by the Cypher Builder.

To create more complex queries, refer to the tutorial on Filtering and projection.