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.

Creating Lots of Nodes & Relationships with Properties

I have a lot of relationships and nodes that I need to add to a Neo4j database with a create function. The function first creates a lot of nodes by parsing parameters and basically using this query:

UNWIND $props AS map
CREATE (n)
SET n = map
SET n:Label1:Label2
RETURN count(n) AS nodeCount

I then verify that the number of nodes to be added (i.e. the number of elements parsed to the function) is equal to the number of nodes that get counted after running the query. That works fine in C# using the Neo4j Driver because I just use:

await _session.WriteTransactionAsync(async tx =>
                {
                    var cursor = await tx.RunAsync(cypherQuery, parameters);
                    returnRecords = await cursor.ToListAsync();
                });

Where parameters is just a JSON file with the parameters for the various nodes.

Then I want to create the relationships but they have a lot of properties, so I would like to use the C# function:

RunAsync(cypherQuery, parameterDictionary)

and just create a dictionary with the form:

{string keyForPropsForRel1, object mapOfPropertiesForRel1},
{string keyForPropsForRel2, object mapOfPropertiesForRel2},
{string keyForPropsForRel3, object mapOfPropertiesForRel3}, ...

Then just parse the items into a cypher query by first matching all the relevant nodes, then unwinding all the parameters into maps, then running all the create functions, then setting the data. So as follows:

MATCH(r1start{id:"guid1"})
MATCH(r1end{id:"guid2"})
MATCH(r2start{id:"guid3"})
MATCH(r2end{id:"guid4"})
...

UNWIND $keyForPropsForRel1 AS mapForRel1
UNWIND $keyForPropsForRel2 AS mapForRel2
...

CREATE (r1start)-[rel1:TypeLabel]->(r1end)
CREATE (r2start)-[rel2:TypeLabel]->(r2end)
...

SET rel1 = mapForRel1
SET rel2 = mapForRel2
...

That does work but it seems a bit tedious. I also haven't thought of a way to check whether it ran successfully. Does anyone have a good way of doing what I'm trying to do? I'm trying to avoid multiple database calls.

0 REPLIES 0