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
-
Define a node variable by creating a new instance of
Cypher.Node
:const movieNode = new Cypher.Node();
-
Create the pattern to be matched against with
Cypher.Pattern
:const matchPattern = new Cypher.Pattern(movieNode, { labels: ["Movie"] });
-
Create a
MATCH
clause and pass the pattern as an argument:const clause = new Cypher.Match(matchPattern);
-
To
RETURN
the Node variables, use the.return
method in theMATCH
clause:clause.return(movieNode);
-
Alternatively, you can do the same thing by creating the clause in a single statement:
const clause = new Cypher.Match(matchPattern).return(movieNode);
-
-
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 |
To create more complex queries, refer to the tutorial on Filtering and projection.