在 Neo4j CALL{ WITH ...} 中使用多个变量

Using multiple variables in Neo4j CALL{ WITH ...}

我有许多节点的属性我想转换成关系和关系属性。理想情况下,我想使用 UNWIND ["prop1", "prop2", ...]CALL { WITH unwound_prop ...} 同时执行其中的许多操作,但是在执行整个 CALL 结构时我 运行 遇到了麻烦。

例如,如果我有几个具有某些属性的人节点:

create (n:person {name: "Tabitha", lunch: "salad", dinner:"steak"});
create (n:person {name: "Tony", lunch: "salad", dinner:"steak"});
create (n:person {name: "Sam", lunch: "spaghetti", dinner:"pizza"});
create (l:meal {name: "lunch"});
create (l:meal {name: "dinner"});

我可以使用以下查询与所有这些人和他们的午餐建立单独的关系(有效):

match (n:person) with n
match (l:meal)
where l.name = "lunch"
and n.lunch IS NOT NULL
create (n)-[r:ATE {food:n.lunch}]->(l)
remove n.lunch;

这会将人员节点的 属性 移动到关系上,这正是我想要的。

但是,如果我想使用 CALL 多次执行此操作,那么我 运行 遇到了一个问题:

unwind ["lunch", "dinner"] as sel_meal
call {
    with sel_meal
    match (n:person) with n
    match (l:meal)
    where l.name = sel_meal
    and n[sel_meal] IS NOT NULL
    create p=(n)-[r:ATE {food:n[sel_meal]}]->(l)
    return p as result
}
return result;

错误:

Neo.ClientError.Statement.SyntaxError: Variable `sel_meal` not defined (line 6, column 20 (offset: 130))
"    where l.name = sel_meal"
                    ^

似乎使用WITH n减少person节点的基数selected覆盖了WITH sel_meal子句,所以我不能使用unwound变量.有没有办法同时包含两者?正确 select 这些节点的更好方法?或者更好的方法将多个变量一次展开为关系(FOREACH 在这里似乎也不是很有用)。

你在中间有一个 WITH 子句,你没有在范围内包含 sel_meal 变量:

match (n:person) with n

整个查询看起来像:

unwind ["lunch", "dinner"] as sel_meal
call {
    with sel_meal
    match (n:person) 
    with n, sel_meal
    match (l:meal)
    where l.name = sel_meal
    and n[sel_meal] IS NOT NULL
    create p=(n)-[r:ATE {food:n[sel_meal]}]->(l)
    return p as result
}
return result;