Querying

This page shows you how to create the following 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 by creating the clause with 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 generates the actual Cypher® string that can be used in a Neo4j database:

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

Conclusion

Your full 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);

If you execute node main.js again, it outputs the following query:

MATCH (this0:Movie)
RETURN this0

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