cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

List all labels NOT starting with "_"

rob2
Node Clone

HI
i want to list all Labels that are NOT starting with underscore ...

MATCH (nodes)
UNWIND nodes AS node
WITH node
WHERE labels(node)! =~ '_*'
RETURN labels(node)

I guess I'm running in the wrong direction .... or?
This gives me an error

Invalid input ' ': expected '=' (line 4, column 20 (offset: 65))
"WHERE labels(node)! =~ '_*'"

and ther must be an distinct somewher
can someone please help me? thanks rob

i also have to say that I'm using multilabel
so match (n) return n, Labels(n); gives me

...
["process", "_ProcessProcess", "_node", "_notdemo"]
...
["status", "_ProcessProcess", "_node", "_notdemo"]
...
["person", "_node", "_notdemo"]
...
["mail", "_node", "_notdemo"]

...

but i need only process, status, mail and person

1 ACCEPTED SOLUTION

I like to use db.labels():

CALL db.labels() YIELD label
WHERE label STARTS WITH "_"
RETURN label

This allows you to call the information stored about the database rather than querying all the nodes. So if you just want the label then this is a good fast way to get it.

View solution in original post

7 REPLIES 7

Hello @rob2

MATCH (nodes)
UNWIND nodes AS node
WITH node
WHERE NOT labels(node) =~ '_*'
RETURN labels(node)

Regards,
Cobra

I like to use db.labels():

CALL db.labels() YIELD label
WHERE label STARTS WITH "_"
RETURN label

This allows you to call the information stored about the database rather than querying all the nodes. So if you just want the label then this is a good fast way to get it.

MHH also have an error here

I edited my original response. Try STARTS WITH. I forgot the S

Ahh thanks, cobra
But still get the same error with your solution

MATCH (nodes)
UNWIND nodes AS node
WITH node
WHERE NOT labels(node) =~ '_*'
RETURN labels(node)

Type mismatch: expected String but was List (line 4, column 11 (offset: 55))
"WHERE NOT labels(node) =~ '_*'"
^ (at "l" of labels)

Yes, it's because labels() returns a list of labels:

MATCH (nodes)
WITH apoc.coll.toSet(apoc.coll.flatten(labels(node))) AS labels
UNWIND labels AS label
WHERE NOT  label=~ '_*'
RETURN label

Regards,
Cobra

Hello there

@mckenzma
Despite the simplicity of the problem, In terms of readability and performance best answer i ever seen in this forum.

Tested on Neo4j 4.2.3

CALL db.labels() YIELD label
WHERE NOT label STARTS WITH '_'
RETURN label

All credit to @mckenzma, except for the NOT part lol.
You should really use the answer above @rob2

I think it's not possible to do better then that. It's short and sweet.