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.

Create nodes conditional properties

Hi
I need help for create a nodes with conditional property.
In this case i have a complex javascript array "similar" to:

[
  {
    type: 'type 1',
    name: 'name 1'
  },
  {
    type: 'type 2',
    name: 'name 2'
  },
  ,
  {
    type: 'type 3',
    name: 'name 3'
  }
]

So, i need create different node for each different type.
example:

CREATE (:OptionData) for type 1
CREATE (:SelectedData) for type 2
....

How can I do it in the best way?
i tried with FORECH, UNWIND, CASE, etc... but i cant.

thanks!!

3 REPLIES 3

Hi @nikosantis.
If I may ask. How are you processing the json? APOC? external code? Perhaps more of your query would help us to troubleshoot. If this is cypher literal collection/objects, then FOREACH with CASE should do the trick.

Ty...

im working in javascript, node js, express, apollo server and graphql.
In the front (ui) with react, i have a Mutation for create use the big object for iterate all data and create nodes and relationships.

session.writeTransaction(
async transaction => {
const cypher = `HERE CYPHER`
const createTxResponse = await transaction.run(cypher, { VARIABLES FOR USE IN CYPHER })
}
)....

I tried this and work:

WITH [{
    type: 'option'
  },
  {
    type: 'comboBox'
  }] AS data
CREATE (form:Form)
SET form.id = randomUUID()
CREATE (section:FormSection)
MERGE (form)-[:HAS_SECTION]->(section)
SET section.id = randomUUID()
FOREACH (comp IN data |
  CREATE (component:Component)
  SET component.id = randomUUID()
  MERGE (section)-[:HAS_COMPONENT]-(component)
  CREATE (dataComponent:DataComponent)
  SET dataComponent.id = randomUUID()
  MERGE (component)-[:HAS_DATA]->(dataComponent)
  FOREACH (i in CASE WHEN comp.type = 'option' THEN [1] ELSE [] END |
    CREATE (optData:OptionData)
    SET optData.id = randomUUID()
    MERGE (dataComponent)-[:IS_DATA_OF]->(optData)
  )
  FOREACH (i in CASE WHEN comp.type = 'comboBox' THEN [1] ELSE [] END |
    CREATE (combo:ComboBoxData)
    SET combo.id = randomUUID()
    MERGE (dataComponent)-[:IS_DATA_OF]->(combo)
  )
)
RETURN data

so, is a good way? i think can be better?