Skip to content

REMOVE clause

Marijn van Wezel edited this page Dec 15, 2022 · 5 revisions

The REMOVE clause is used to remove properties from nodes and relationships, and to remove labels from nodes.

Query::remove(Label|Property|(Label|Property)[] $expressions): Query

Parameters

  • $expressions : A single property or label to remove, or a non-empty list of properties and labels to remove.

Relevant methods

  • addExpression(Label|Property ...$expressions): self : Add one or more labels and properties to remove.

Examples

Return nodes

$n = node()->withProperties(['name' => 'B']);
$query = query()
    ->match($n)
    ->returning($n)
    ->build();

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

Return relationships

$r = relationshipTo()->addType('KNOWS');
$query = query()
    ->match(node()->withProperties(['name' => 'A'])->relationship($r, node()))
    ->returning($r)
    ->build();

$this->assertStringMatchesFormat("MATCH ({name: 'A'})-[%s:KNOWS]->() RETURN %s", $query);

Return property

$n = node()->withProperties(['name' => 'A']);
$query = query()
    ->match($n)
    ->returning($n->property('name'))
    ->build();

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

Variable with uncommon characters

$n = node()->withVariable('This isn\'t a common variable name');
$query = query()
    ->match($n)
    ->where($n->property('name')->equals('A'))
    ->returning($n->property('happy'))
    ->build();

$this->assertSame("MATCH (`This isn't a common variable name`) WHERE (`This isn't a common variable name`.name = 'A') RETURN `This isn't a common variable name`.happy", $query);

Column alias

$a = node()->withProperties(['name' => 'A']);
$query = query()
    ->match($a)
    ->returning($a->property('age')->alias('SomethingTotallyDifferent'))
    ->build();

$this->assertStringMatchesFormat("MATCH (%s {name: 'A'}) RETURN %s.age AS SomethingTotallyDifferent", $query);

Other expressions

$a = node()->withProperties(['name' => 'A']);
$query = query()
    ->match($a)
    ->returning([$a->property('age')->gt(30), "I'm a literal"])
    ->build();

$this->assertStringMatchesFormat("MATCH (%s {name: 'A'}) RETURN (%s.age > 30), 'I\\'m a literal'", $query);

Unique results

$b = node();
$query = query()
    ->match(node()->withProperties(['name' => 'A'])->relationshipTo($b))
    ->returning($b, true)
    ->build();

$this->assertStringMatchesFormat("MATCH ({name: 'A'})-->(%s) RETURN DISTINCT %s", $query);

External links

Clone this wiki locally