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.

Is there a way to set a parameter equal to query results?

tj_a
Node Link

Hey all,

Wondering if you can create params equal to query results.
For example, takethe creation of stringList:

:param  stringList => ["string1", "string2", "string3"]

Is there a way to create stringList equal to the result from:

match(ex:example)
return collect(ex.someStringProperty) as stringList
1 ACCEPTED SOLUTION

I think the narrow specific answer to your question is "no" -- but there's a way you can get what you want.

Instead of doing this:

:param  stringList => ["string1", "string2", "string3"]
RETURN $stringList;

Just do this:

match(ex:example)
WITH collect(ex.someStringProperty) as stringList
(your cypher here)

In other words, just run it as a pre-query, bind it to "stringList" as a variable in a cypher query, and then it functions the same as if it were a parameter, but you compute it each time.

If you don't want to compute it each time (for example because it's a big list of strings) you could compute it once, put it into a temp node, and then start with that node's value.

match(ex:example)
WITH collect(ex.someStringProperty) as stringList
CREATE (:Cache { stringList: stringList });

And then later:

MATCH (c:Cache)
WITH c.stringList as stringList
(your cypher here)

View solution in original post

3 REPLIES 3

I think the narrow specific answer to your question is "no" -- but there's a way you can get what you want.

Instead of doing this:

:param  stringList => ["string1", "string2", "string3"]
RETURN $stringList;

Just do this:

match(ex:example)
WITH collect(ex.someStringProperty) as stringList
(your cypher here)

In other words, just run it as a pre-query, bind it to "stringList" as a variable in a cypher query, and then it functions the same as if it were a parameter, but you compute it each time.

If you don't want to compute it each time (for example because it's a big list of strings) you could compute it once, put it into a temp node, and then start with that node's value.

match(ex:example)
WITH collect(ex.someStringProperty) as stringList
CREATE (:Cache { stringList: stringList });

And then later:

MATCH (c:Cache)
WITH c.stringList as stringList
(your cypher here)

Thanks David!

The main reason I was asking is mainly for when I'm debugging and need to use a parameter that requires some complex pre-query. Which then requires copy/pasting the pre-query for each different query I'm running relying on that param.

I had not thought about just saving the result to a temp node as you suggested which solves that problem for the most part!

Ideally I'd love to see being able to assign params to the results of a query as a feature in the future, as it saves extra steps needed to either assign the param or extra code to match on (:cache) for each query.
Perhaps I'll put that in as a feature request.

if your pre-query is not so complex you could use e.g. pattern comprehensions or similar to turn it into an expression.

the => variant of param is actually executing a Cypher call RETURN expr
so even stuff like this works. :param foo => apoc.coll.toSet([1,1,1])

if you want to wrap a complex cypher query you can try:

:param foo => apoc.cypher.runFirstColumnMany('MATCH (n) RETURN id(n)',{})

or even with parameters:

:param foo => apoc.cypher.runFirstColumnMany('MATCH (n) WHERE id(n) < {min} RETURN id(n)',{min:12})