32

I am trying to run queries from the neo4j browser to reproduce results from my neo4j-javascript-driver client.

What is the syntax for defining query parameters in the neo4j b

I recently attended a neo4j training session in NYC where the trainer (David Fauth) did this, unfortunately, I did not take notes on it, since I figured that I could read-up on this online...but no success.

Joel Stevick
  • 1,638
  • 2
  • 16
  • 22
  • I recently attended a neo4j training session in NYC where the trainer (David Fauth) did this, unfortunately, I did not take notes on it, since I figured that I could read-up on this online...the question you referenced was from 2015. – Joel Stevick Feb 22 '17 at 17:38
  • Yes, you are right. – stdob-- Feb 22 '17 at 17:50

5 Answers5

50

In neo4j-browser you need type for example:

:params {nodes: [{name: "John", age: 18}, {name: "Phill", age: 23}]}

Then you can use params as usual:

UNWIND {nodes} as node
MERGE (A:User {name: node.name, age: node.age})
RETURN A

For clear params in neo4j-browser type :params {}.

For additional help type :help params.

Davey
  • 2,355
  • 1
  • 17
  • 18
stdob--
  • 28,222
  • 5
  • 58
  • 73
  • Great, that is exactly what I was looking for, thank you! – Joel Stevick Feb 22 '17 at 17:54
  • 2
    you can also use `:param name: value` where value can be any valid json – Michael Hunger Feb 22 '17 at 19:27
  • What version did this feature release with? – Merrick Apr 24 '17 at 18:29
  • This doesn't support nested properties. For example this will fail: `:params {"uidLeft": "asdf1", "uidRight": "asdf2", "type": "KNOWS", props:{uid:"rel1"}}` – Clintm Sep 15 '17 at 15:00
  • @Clintm What version are you using? I tried your example on version 3.2.1 (community) and everything works fine. – stdob-- Sep 15 '17 at 15:17
  • Asked a new question here: https://stackoverflow.com/questions/46244571/unable-to-set-nested-parameters-through-the-neo4j-browser-with-version-3-2-2 – Clintm Sep 15 '17 at 17:14
  • This fails for me with Neo4j 3.3.4: `:params { location:{latitude:42.3600825,longitude:-71.0588801} }` – Jackpile Apr 06 '18 at 16:20
  • 1
    @Jackpile remove the { after params and } in the end of the line – Aerodynamika Jan 21 '19 at 14:53
  • with the latest neo4j 4.3.7, accessing params via { } is depreciated, and instead of you can use $. The same code can be rewritten as `UNWIND $nodes as node MERGE (A:User {name: node.name, age: node.age}) RETURN A` – Shyam Pratap Singh Nov 28 '21 at 13:30
10

In Neo4j-3.3.4, the cypher likes this:

:param nodes: [{name: 'John', age: 18}, {name: 'Phill', age: 23}]

Neo4j Browser result: here

Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
Mr.QA
  • 101
  • 1
  • 3
  • Now this is recommended answer. `params` seems to be a footgun. – Cerberus Jul 31 '19 at 05:17
  • Still true for neo4j-3.5.8. But it's only in Neo4j Browser docs, and mentioned nowhere in Cypher doc or Cypher spec. Any clue _where_ this is suppoced be be specified? – Tony Chiboucas Aug 20 '19 at 20:32
2

In Neo4j Browser 3.5+ you can use the Cypher Shell parameter syntax, documented here: https://neo4j.com/docs/operations-manual/3.5/tools/cypher-shell/#cypher-shell-parameters

:param name => expression

The expression must be kept on a single line.

The expression could be a scalar or a list:

:param foo => ['a', 'b', 'c']

Maps can't be used directly with this syntax as of Neo4j 4.1. You can wrap them into a list:

:param foo => [{name: 'Alice', age: 38, address: {city: 'London', residential: true}}] 

Or you can use :params:

:params {foo: {name: 'Alice', age: 38, address: {city: 'London', residential: true}}}
Dima Korobskiy
  • 1,479
  • 16
  • 26
  • This is only true for single values. For objects, Neo4j Browser still requires you to use the syntax in the accepted answer. – Benjamin R Nov 09 '20 at 07:38
  • It works for lists as well. Not for maps though, although I can wrap a map inside a list, e,g,: `:param foo => [{name: 'Alice', age: 38, address: {city: 'London', residential: true}}]`. I've updated the answer. Thanks for pointing this out! – Dima Korobskiy Nov 12 '20 at 18:40
0

In Neo4j Browser 3.5+ you can use

:params param_name => 'param_value'
0

The basic cases:

  1. Set singe variable value mytext="Hello": :param mytext => "Hello"
  2. set dictionary values attr={"oid":1, "text":"Hello"}: :param attr => ({oid: 1, text:"Hello"})

Cypher usage:

  1. MATCH (x) WHERE x.a = $mytext RETURN x
  2. set a value MATCH (x) WHERE x.a = $attr.oid SET x.b = $attr.text
  3. set multiple values MATCH (x) WHERE ... SET x = $attr
Juha M
  • 380
  • 3
  • 13