Suppose I have a JSON file like this:
{
data: [
{
name: "1",
description: "Node 1",
parent: ""
},
{
name: "2",
description: "Node 2",
parent: "1"
},
{
name: "3",
description: "Node 3",
parent: "1"
},
{
name: "4",
description: "Node 4",
parent: "3"
},
]
}
I wish to create the nodes and the relationships in the same query using neo4j-driver for Node.js. Suppose I have that JSON file loaded in a variable called data. I've tried this, but haven't been able to create the relationships:
const result = await session.run(`
UNWIND $nodes AS node
MERGE (n:Node { name: node.name, description: node.description })
WITH $nodes AS relationships
UNWIND relationships AS relationship
MATCH(c:Node { name: relationship.name }), (p:Node { name: relationship.parent })
MERGE (p)-[:PARENT_OF]->(n)
`, { nodes: data });
I get two relationships from 1 to a null node and from 2 to a null node.
Is there any way to do this or should I first create the nodes and then in another query create the relationships.
Thank you