Williams
Williams

Reputation: 4328

Neo4j all the ways to run a `MATCH`

I'm trying to fill in my understanding of the fundamentals of neo4j (version 3.4).

I believe that all of the following produce the same results -- ie they are different syntax for doing exactly the same thing:

MATCH (ee {name: "Emil"}) RETURN ee;
MATCH (ee) WHERE ee.name = "Emil" RETURN ee;
MATCH (ee:Person {name: "Emil"}) RETURN ee;
MATCH (ee:Person) WHERE ee.name = "Emil" RETURN ee;
MATCH (ee:Person) WHERE (ee).name = "Emil" RETURN ee;

I actually have several questions:

Upvotes: 1

Views: 41

Answers (2)

David A Stumpf
David A Stumpf

Reputation: 793

For an exhaustive list, you might include:

MATCH (ee) WHERE ee.name in ["Emil"] RETURN ee

This is not particularly helpful with a single item, but you can have multiple comma delimited items in a list or a collection. For example,

match (n:order{item:'widget'}) 
with collect(distinct n.customer_id) as customerCollection
match (c:Customers) where c.customer_id in [customerCollection]
return c.Name, c.City

Upvotes: 1

cybersam
cybersam

Reputation: 67019

  1. Your clauses are NOT all the same.

    The first group of (2) MATCH clauses DO NOT require the Person label for matched nodes. But the second group of (3) clauses DO require the Person label.

  2. Putting a label (or many labels, as appropriate) on a node is generally a good idea, as matching on a label is a quick way to filter your nodes when performing a MATCH. And labels also aid in data model understandability. In addition, a label is required if you wanted to index a node property.

    But whether or not you should specify a label (or several) on a specific a MATCH clause depends on the amount of filtering you are trying to do. Omitting all labels from a MATCH would be perfectly appropriate if at that point in your query you really wanted to match nodes with any label (or even no labels).

Upvotes: 1

Related Questions