How to use a variable in after the Return statement

Hello All,

I wanted to write a query that can give an attribute name search where it has an attribute name matching a provided name.

For example, return the nodes where any node has an attribute e.g. named "value". Not a value of "value" but an attribute named value.

The result list should output

"The following nodes have attributes named "value":

Bow this heading, show results in rows where the columns of the rows shown are:

node id, node label(s)
The result list should output
"The following nodes have attributes named "value":

Obviously replace “value” with whatever the attribute name was given in query Preformatted text
Query:
with "value" as m
match (n) where m in keys(n)
return n as The following nodes have attributes named+[m]

I have checked the google and Neo4j docs but couldn't find a way to do that. Any help would be appreciated!
Thank you in advance!

Thanks,
Rishabh

Have you tried treating the node as a property map? This should work:

WITH "value" as propertyName
MATCH (u) 
WHERE u[propertyName] IS NOT NULL
RETURN u;

The key trick here is that properties can be used like maps; with each property name (in this case, a variable called propertyName acting as a key

@david_allen Thank you for response! but looks like I couldn't explain it properly in the post.

My question is how we can return "value" / propertyName in return statement:

Something like : RETURN u as The following nodes have attributes named value/propertyName

Obviously replace “value” with whatever the attribute name was given in query

Thanks,
Rishbah

Perhaps I still don't understand, but you can do this with simple string concatenation:

WITH "value" as propertyName
RETURN "The following nodes have attributes named " + propertyName;

I tried the string concatenation but that didn't work. I get below error when I try to concatenate.

I want something like this. Where I have written manually "value"

Try this:

match (c) where exists (c.value)
with collect(distinct labels(c)) as lbls
return 'the following labels have value as property:' + lbls

@ameyasoft No luck, We are not able to concatenate anything after the return statement. Not sure if this is possible or not in Neo4j.

You are trying return node 'n' as a string and this is where it's failing.
Use RETURN `The following nodes have attributes named` +[m]

I think, I am not able to explain my problem. I able to run below query successfully with out any error and it is giving the required nodes but my client want specific text "The following nodes have attributes named: value". "Value" is the string which user is searching. So we can't hard code it. So I want searched string at the end but not getting the way to do.

with "value" as m
match (n) where m in keys(n)
with n, collect(distinct labels(n)) as lbls
return n as `The following nodes have attributes named:`

Try this:

match (c) where exists (c.value)
with collect(distinct labels(c)) as the_following_labels_have_value_as_property
return the_following_labels_have_value_as_property

@ameyasoft Thanks for answering this but I will request you to read my post and comments so that you can understand my problem.

I think I see the problem.

This doesn't really have to do with what's happening before or after a RETURN, this has to do with limitations for how we are allowed to alias variables in Cypher, since what you're actually trying to do is alias a variable such that the variable is assembled dynamically.

And that currently isn't allowed. Aliases for variables must be hard coded in the query, they cannot be dynamic.

What you could do, in your client code that is going to make the query to Neo4j, is to dynamically append the value into the query string before you send that off to Neo4j for execution, so once it reaches Neo4j, it is essentially hardcoded.

Or, if you want to handle this entirely within the Cypher query itself, you can create a map with a dynamic key, and return that map, but you'll have to use some functions from APOC Procedures to do this:

WITH "value" as m
MATCH (n) 
WHERE m in keys(n)
WITH m, collect(n) as nodes
WITH nodes, "The following nodes have attributes named '" + m + "'" as key
RETURN apoc.map.fromValues([key, nodes]) as result

That's as close as you can get.