Is this valid fluent cypher? (newbie code)

27 views
Skip to first unread message

Jacqui Read

unread,
Nov 6, 2013, 4:24:22 AM11/6/13
to neo4j...@googlegroups.com
Hi all,

I have just started with Neo4j, Cypher and Neo4jClient and I have created the following code. 

I have three questions really: 
1. Is the second cypher, in version 1, a valid way to create a relationship with the root node (and should I bother)? 
2. Is the second piece of code, combining the two operations, valid?
3. Is there a way to create multiple nodes with just one cypher call? i.e. not have to use the foreach loop and call the database just once to insert all apps in the list. I am thinking there probably isn't due to the nature of cypher.

Many thanks for your help and time :)
Jacqui

Version 1
private void CreateAppsForRoot(List<App> apps)
		{
			if (apps != null && apps.Any())
			{
				// Create app and relationship only if they don't already exist
				foreach (var a in apps)
				{
					// Create App
					client.Cypher
					.Merge("(app:App {Id: {id}})")
					.OnCreate("app")
					.Set("app = {newApp}")
					.WithParams(new { id = a.Id, newApp = a })
					.ExecuteWithoutResults();
					// Create Relationship
					client.Cypher
					.Match("(app:App)")
					.Where((App app) => app.Id == a.Id)
					.CreateUnique("app-[:APP_OF]->{root}")
					.WithParam("root", client.RootNode)
					.ExecuteWithoutResults();
				}
			}
		}


Version 2
private void CreateAppsForRoot(List<App> apps)
		{
			if (apps != null && apps.Any())
			{
				// Create app and relationship only if they don't already exist
				foreach (var a in apps)
				{
					// Create App and Relationship
					client.Cypher
					.Merge("(app:App {Id: {id}})")
					.OnCreate("app")
					.Set("app = {newApp}")
                                        .CreateUnique("app-[:APP_OF]->{root}")
.WithParams(new { id = a.Id, newApp = a, root = client.RootNode })
					.ExecuteWithoutResults();
				}
			}
		}


Reply all
Reply to author
Forward
0 new messages