Match

This page describes how to create a MATCH clause with the Cypher®.Match class.

To add the MATCH clause, first create a valid pattern using the Pattern class and pass it to the Match constructor:

const movie = new Cypher.Node();
const pattern = new Cypher.Pattern(movie, { labels: ["Movie"] });

const matchQuery = new Cypher.Match(pattern);
const { cypher, params } = matchQuery.build()

This generates the following MATCH clause:

MATCH (this:Movie)

Afterwards, other clauses can be added. For example:

const matchQuery = new Cypher.Match(pattern)
    .where(Cypher.eq(movie.property("name"), new Cypher.Param("my-movie")))
    .return(movie);

You can also chain extra match clauses:

const matchQuery = new Cypher.Match(pattern).match(pattern2).return(movie)

Relationships

You can use relationships in a match clause by creating the relevant Pattern:

const actedInPattern = new Cypher.Pattern(personNode, { labels: ["Person"] })
    .related({ type: "ACTED_IN" })
    .to(movieNode, { labels: ["Movie"] });

const matchQuery = new Cypher.Match(actedInPattern)
MATCH (this0:Person)-[:ACTED_IN]->(this1:Movie)

Multiple patterns

You can match multiple patterns in a single MATCH clause by passing multiple patterns to the Match constructor:

const actor = new Cypher.Node();
const moreActors = new Cypher.Node();
const movie = new Cypher.Node();

const pattern1 = new Cypher.Pattern(actor, { labels: ["Person"] })
    .related({ type: "ACTED_IN", direction: "undirected" })
    .to(movie, { labels: ["Movie"] });

const pattern2 = new Cypher.Pattern(moreActors, { labels: ["Person"] })
    .related({ type: "ACTED_IN", direction: "undirected" })
    .to(movie);

const matchQuery = new Cypher.Match(pattern1, pattern2).return(actor, moreActors, movie);
MATCH
  (this0:Person)-[:ACTED_IN]-(this1:Movie),
  (this2:Person)-[:ACTED_IN]-(this1)
RETURN this0, this2, this1

Filtering with WHERE

A WHERE clause can be appended with the .where method, this accepts any predicate:

new Cypher.Match(new Cypher.Pattern(movieNode, { labels: ["Movie"] }))
   .where(Cypher.eq(node.property("title"), new Cypher.Param("Matrix")));
MATCH (this0:Movie)
WHERE title = $param0

You can chain multiple predicates by using the .and or .where method after .where:

new Cypher.Match(new Cypher.Pattern(movieNode, { labels: ["Movie"] }))
   .where(Cypher.eq(node.property("title"), new Cypher.Param("Matrix")))
   .and(Cypher.neq(node.property("year"), new Cypher.Param(2001)));
MATCH (this0:Movie)
WHERE this0.title = $param0
AND this0.year <> $param1

Logical filters

For more complex logical filters, use the Cypher®.and, Cypher.or, Cypher.not and Cypher.xor predicates inside where:

new Cypher.Match(new Cypher.Pattern(movieNode, { labels: ["Movie"] }))
    .where(
        Cypher.and(
            Cypher.or(Cypher.eq(movieNode.property("title"), new Cypher.Param("Matrix")), Cypher.gt(movieNode.property("year"), new Cypher.Param(1990))),
            Cypher.neq(movieNode.property("year"), new Cypher.Param(2001))
        )
    )
MATCH (this0:Movie)
WHERE ((this0.title = $param0 OR this0.year > $param1) AND this0.year <> $param2)

Filtering properties shorthand

.where also supports passing a variable and an object with variables to check for equality of multiple values in a more concise way:

const matchQuery = new Cypher.Match(new Cypher.Pattern(movieNode, { labels: ["Movie"] }))
    .where(movieNode, { id: idParam, name: nameParam })
MATCH (this0:Movie)
WHERE (this0.id = $param0 AND this0.name = $param1)

Optional match

You can create the clause OPTIONAL MATCH similarly to MATCH by using the class Cypher®.OptionalMatch:

const movie = new Cypher.Node();
const pattern = new Cypher.Pattern(movie, { labels: ["Movie"] });

const matchQuery = new Cypher.OptionalMatch(pattern);

Alternatively, you can transform an existing Match instance into an OptionalMatch with the method .optional():

const movie = new Cypher.Node();
const pattern = new Cypher.Pattern(movie, { labels: ["Movie"] });

const matchQuery = new Cypher.Match(pattern).optional();

Shortest paths

You can add Shortest paths and its variations in a Match clause with the following methods in Cypher®.Match:

  • .shortest(k)

  • .shortestGroups(k)

  • .allShortest

  • .any

For example:

const movieNode = new Cypher.Node();

const matchQuery = new Cypher.Match(
    new Cypher.Pattern(movieNode, {
        labels: ["Movie"]
    })
        .related()
        .to(new Cypher.Node(), {
            labels: ["Person"],
        })
)
    .shortestGroups(2)
    .return(movieNode);
MATCH SHORTEST 2 GROUPS (this0:Movie)-[this1]->(this2:Person)
RETURN this0