cancel
Showing results for 
Search instead for 
Did you mean: 

Create New Nodes from Array

RedInk
Node Link

Greetings,

Using node.js and Express

I have an input field name="tablenames" and I can pass this to my server using const {tablenames} = req.body and I can console.log and see an array of the expected table names...

console.log(tablenames);

[table1, table2, table3, table4, table5]

I am trying to take the tablenames array and build 5 new nodes each with the .Title of a given table seen in the array.

I've tried the following queries, but am at a loss as to how to do what I thought would be a fairly simple task.

Try 1

 

Merge (t:Table {Title: $tablenames})

 

The result of this is 1 new node created with all 5 of the table1~5 as the .Title

Try 2

 

UNWIND $tablenames AS map
CREATE (n:Table)
SET n = map.Title

 

I realize I'm not using map correctly as it return an error, but I'm trying to follow the Create documentation for creating multiple nodes.

How do I create these 5 separate nodes from the array and give them the titles listed in the array?

Many Thanks!

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

Try this:

 

UNWIND $tablenames AS table
CREATE (n:Table)
SET n.Title = table

Or this:

forEach(i in $tablenames |
CREATE (n:Table)
SET n.Title = i)

 

View solution in original post

2 REPLIES 2

glilienfield
Ninja
Ninja

Try this:

 

UNWIND $tablenames AS table
CREATE (n:Table)
SET n.Title = table

Or this:

forEach(i in $tablenames |
CREATE (n:Table)
SET n.Title = i)

 

RedInk
Node Link

Perfect, thank you!

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.