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 node if given unique property is not null

I have a use case where
Create new node if there is no node with given id
Return existing node if node exists with given id
return null if given id is null

Ex: User({emailId: 'x.com'})
If email id is null, then it should return null

1 ACCEPTED SOLUTION

solved this way

foreach(
                managerEmailId in CASE when ${'$'}managerEmailId IS NOT NULL THEN[${'$'}managerEmailId] ELSE [] END | 
                merge (u)<-[:IS_MANAGER_OF]-(:User {email_id : managerEmailId, tenant_id: ${'$'}tenantId})
            )
            with u

View solution in original post

3 REPLIES 3

merge https://neo4j.com/docs/cypher-manual/4.2/clauses/merge/ will do create if not exists otherwise return but does not account for if null.

For example

merge (n: User({emailId: 'x.com'}) return n;

will create the node and then return the node if it the node does not already exist and if the node already exists it will simply return the node and not create a new node.

Note the documentation link above refers to Neo4j 4.2 documentation. It should be the same for all versions of Neo4j but as the initial problem description provided no version details

There is a problem with syntax of your merge statement.

solved this way

foreach(
                managerEmailId in CASE when ${'$'}managerEmailId IS NOT NULL THEN[${'$'}managerEmailId] ELSE [] END | 
                merge (u)<-[:IS_MANAGER_OF]-(:User {email_id : managerEmailId, tenant_id: ${'$'}tenantId})
            )
            with u