Filtering a node that only has 1 type of label

Hi all,
I am trying to filter nodes that only have 1 particular type of label.
Example:

CREATE (a:Label1:Label2 {name:'Test 1'})
CREATE (b:Label1 {name: 'Test 2'})

In this example, I would like to return all the nodes that only have the 1 label (Label1). I tried using the query below.

MATCH (n:Label1)
RETURN n

However it would return both nodes, but I only want to return the node with label (Label1) only.
Thanks in advance for the help.

Hello @tarendran.vivekanand :slight_smile:

Try:

MATCH (n:Label1)
WHERE NOT n:Label2
RETURN n

Regards,
Cobra

1 Like

Hello @Cobra :slight_smile:,
Thanks for the solution.
I was also wondering if it would it be better to have a unique property for the different nodes and using EXISTS to match for the nodes I am looking for? Would this be faster also when matching?

No problem :slight_smile:

Do you have an example of property you would like to use?
To see if it's faster, you will have to compare with PROFILE and EXPLAIN, sometimes it really depends of your use case :slight_smile:

I thought of giving the property a prefix then indexing it so that the match would be faster.
Example

CREATE (a:Label1:Label2 {l1_l2_name:'Test 1'})
CREATE (b:Label1 {l1_name: 'Test 2'})

However I believe this would take up more space (i.e. wasting space) though not sure about this?

To be honest, I prefer to not add a property, your model will be dirty, but if you need performance, you can compare to see if there are any speed improvements :slight_smile:

1 Like

Thanks @Cobra. :slight_smile:
Definitely will look more into this.

1 Like