Reputation: 2025
I have following data (Info) in Neo4j Label:
{
domain:"domain1.com",
email: "[email protected]"
},
{
domain:"domain2.com",
email: "[email protected]"
}
..................
..................
Now I want to insert data into Info if new domain is found, otherwise merge.
I've tried the following CQL:
WITH [{domain:"domain1.com", email: "[email protected]"},
{domain:"domain3.com", email: "[email protected]"}] as arr
UNWIND arr as ar
MATCH (x:Info) WHERE x.domain = ar.domain
WITH count(x) as c, ar
CALL apoc.do.when( c <> 0,
'MERGE (a:Info {domain: ar.domain}) ON SET a.email=ar.email return a',
'CREATE (a:Info { domain: ar.domain, email: ar.email }) return a',
{ c:c, ar:arr }) YEILD value
RETURN value
In above CQL
, only merge is working, but nothing created.
Any suggestion?
Thanks in advance.
Upvotes: 0
Views: 1107
Reputation: 30407
You're mixing up ar
and arr
all over the place here.
In your outer query arr
is a list. Yet you pass arr
in for ar
in your apoc.do.when()
call and treat it as a map (ar.domain
and ar.email
obviously won't work here within apoc.do.when()
)
You're also referencing arr.domain
and arr.email
in the else part of apoc.do.when()
, but arr
isn't in scope.
You also don't need to pass c
into the procedure call; the evaluation in the first argument uses the existing scope, only the queries after that require you to pass variables between scope.
Also ON SET
isn't valid, you have to use ON MATCH SET
, ON CREATE SET
, or just SET
.
All this said, you don't even need to use apoc.do.when()
, it looks like you only need to use MERGE and SET:
WITH [{domain:"domain1.com", email: "[email protected]"},
{domain:"domain3.com", email: "[email protected]"}] as arr
UNWIND arr as ar
MERGE (x:Info {domain:ar.domain})
SET x.email = ar.email
RETURN x
Remember that MERGE is like a MATCH or CREATE (when no matches are found), so after the MERGE it will always be bound to a node, either an existing node or a newly created node, and since you want to set the email
property in either case, we can just use SET here.
Upvotes: 1