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.

How to pass dynamic properties in merge query using $props

Hi,

How to avoid creating multiple nodes in neo4j if properties are the same?

Currently, I am using create a function to create the nodes but with this, it is creating multiple nodes every time.

query = "CREATE (o:Organization $props) RETURN o"
nodes = session.run(query, props=(data))

I tried to merge instead of creating but it is showing an error.

query = "Merge (o:Organization $props) RETURN o"
nodes = session.run(query, props=(data))

ERROR - {message: Parameter maps cannot be used in MERGE patterns (use a literal map instead, eg. "{id: {param}.id}")

** data is a dynamic dictionary

Thanks &Regards
Raja

4 REPLIES 4

Hi @kushwah.raja1

MERGE (o:Organization {id: {param}.id})
SET o.prop1 = {param}.prop1
SET o.prop2 = {param}.prop2

As you can see, it is a better design to write key properties and others separately.
MERGE searches for the same node by label and parameter.
If there is data, it updates it. If not, it creates it.
Usually, the search items are given an index.
If you write all the items here, you will have to index all the items, which is bad design.

Hi @koji,

My dict(data) looks like - {'id': '410a9bc9-2dfe-332e-9662-9377baa65697', 'name': 'ALLINA HEALTH SYSTEM'} , the values associated with id and name is dynamic.

I am new to neo4j so can you please suggest to me how to form merge with this param?

I tried the below queries but not getting how to do it.

data={dynamic dict}
query="MERGE(o: Organization{id: {$data}.id}) SET o.id = {$data}.id SET o.name = {$data}.name"
nodes = session.run(query,param=data)

Expected result - Create a node with parameters id, name

Hi @kushwah.raja1

My code "{param}.id" was wrong.

If two parameters are used as keys, the code will look like this.

CREATE INDEX org_id_name FOR (o:Organization) ON (o.id, o.name)

The param is this.

:params {"data" : {"id" : "A123","name" : "Andy"}}
{
  "data": {
"id": "A123",
"name": "Andy"
  }
}

The Cypher is like this.

MERGE (o:Organization {id: $data.id, name: $data.name})
RETURN o

Hi @koji,

I am using python code to execute the query. Please find the below code and help me if I am missing something.

code -

    for data in organizations:
      query="MERGE (o:Organization {id: $data.ID, name: $data.Name}) return o"
        nodes = session.run(query,props=data)

data looks like - {'ID': '410a9bc9-2dfe-332e-9662-9377baa65697', 'Name': 'ALLINA HEALTH SYSTEM', 'Address': '1221 W LAKE ST'}

ERROR- raise Neo4jError.hydrate(**metadata)
neo4j.exceptions.ClientError: {code: Neo.ClientError.Statement.ParameterMissing} {message: Expected parameter(s): data}