Skip to content

MATCH clause

Marijn van Wezel edited this page Dec 9, 2022 · 8 revisions

The MATCH clause is used to search for the pattern described in it. It accepts a list of patterns to match on.

Query::match(CompletePattern|CompletePattern[] $patterns): Query

Parameters

  • $patterns : The patterns to match on.

Examples

Get all nodes

$n = Query::node();
$query = Query::new()
    ->match($n)
    ->returning($n)
    ->build();

$this->assertStringMatchesFormat("MATCH (%s) RETURN %s", $query);

Get all nodes with a label

$movie = Query::node("Movie");
$query = Query::new()
    ->match($movie)
    ->returning($movie->property("title"))
    ->build();

$this->assertStringMatchesFormat("MATCH (%s:Movie) RETURN %s.title", $query);

Related nodes

$movie = Query::node();
$query = Query::new()
    ->match(Query::node()->withProperties(['name' => 'Oliver Stone'])->relationshipUni($movie))
    ->returning($movie->property("title"))
    ->build();

$this->assertStringMatchesFormat("MATCH ({name: 'Oliver Stone'})--(%s) RETURN %s.title", $query);

Match with labels

$movie = Query::node('Movie');
$query = Query::new()
    ->match(Query::node('Person')->withProperties(['name' => 'Oliver Stone'])->relationshipUni($movie))
    ->returning($movie->property("title"))
    ->build();

$this->assertStringMatchesFormat("MATCH (:Person {name: 'Oliver Stone'})--(%s:Movie) RETURN %s.title", $query);

Match with a label expression for the node labels

The more complex label expression syntax added in Neo4j 5.0 is not yet supported.

$n = Query::node()->addLabel('Movie', 'Person');
$query = Query::new()
    ->match($n)
    ->returning(['name' => $n->property("name"), 'title' => $n->property("title")])
    ->build();

$this->assertStringMatchesFormat("MATCH (%s:Movie:Person) RETURN %s.name AS name, %s.title AS title", $query);

External links

Clone this wiki locally