Return node with properties and exclude some of them via Cypher


Let's assume we have a node with a label called A


which returns one node with those properties:

prop1: "value1",
prop2: "value2",
prop3: "value3",
prop4: "value4"

The node can have n properties, but for the sake of simplicity, let's say we have 4 properties, as listed here.

Is there a way to write a Cypher query that I can get this node without a specific property? For example, I don't want to get property prop4 in the resulting node from the return statement of the query (we are talking about the same node).

I know there is a way to access those properties and send them to the server

MATCH (a:A) RETURN a.prop1, a.prop2, a.prop3

but, as you can imagine, if there were more properties, it would be a rather long query to disregard just one property in the result. Imagine if there were 50 properties and I want to exclude one of them in the result, so I have to write 49 properties in one query, which I don't think is the right way.

So, I would like to set a collection of properties that will return the node with those properties, excluding other properties that it has (like to get a subset of properties from the node) in the result (not affecting the match). How can I achieve this?

The result I would like to get from said query is

prop1: "value1",
prop2: "value2",
prop3: "value3"

with prop4 excluded!

Thanks in advance!

match (n:A) 
with keys(n) as k1, n 
unwind k1 as k2
with n,  k2 where k2 <> "prop4"
return k2 as Prop,  n[k2] as value

This will give you the desired result.

hi @said,

Welcome to the Neo4j Community!!

You can use NOT EXISTS predicate function as

MATCH (a: A) WHERE NOT EXISTS ( a.prop4) RETURN a returns none as the node A has prop4 as a property. If there is another node with the same label and no prop4 as a property then it returns the node.

@said requirement is to hide or not display selected property or properties.

1 Like

Oh yes.. you are right!!.
However in that case returning only required properties is much better and cheaper, I am not sure why @said is not interested in that. Expanding all nodes with properties and then segregating selected properties is costlier business. But is then only solution I feel so too..

Thanks for correcting me!! :smiley:

@ameyasoft Thank you so much, this solves the problem!

Greetings, @intouch.vivek!

I appreciate your welcome!

I agree that it is the preferred way to return properties you need, but as I have said in the post, the problem I see with this way would be that I need to extract a lot of properties in the query just to exclude one of them. Imagine if I needed to get 49 properties out of 50 (or even more) from the nodes, I believe that would be a struggle to write such a query.

What are your suggestions? Should I write them all properties in the query and what are the advantages of doing so?

Thank you!


If the expectation is that in the UI you are looking 48 properties and do not need the rest 2 other properties, then you can return complete node along with all the properties and then filter those 48 properties using your programming language.
In case you have 50 properties of a node and you need just 35 then if possible, try to return only required properties it all depends on the exact use case.
We need to see that what exactly you are storing on those poperties.
If the property you want to forbid from is not fixed then @ameyasoft has given you the solution for the same too.

1 Like