简体   繁体   English

在 Neo4J 中切换关系

[英]Toggle relationship in Neo4J

I'm trying to implement follow/unfollow in Neo4J.我正在尝试在 Neo4J 中实现关注/取消关注。 I would like to write a query would toggle the relationship between two nodes.我想写一个查询来切换两个节点之间的关系。

I currently have the following query:我目前有以下查询:

neoSession.writeTransaction(tx => tx.run('MATCH (me:User), (other:User) WHERE ID(me) = $me AND ID(other) = $other OPTIONAL MATCH (me)-[af:FOLLOWS]->(other) CALL apoc.do.when(af IS NULL, CREATE (me)-[f:FOLLOWS]->(other), DELETE af)', { me: req.user_id, other: req.body.user, datetime: Date.now() }));

Prettified query-only:美化查询:

MATCH (me:User), (other:User) 
  WHERE ID(me) = $me AND ID(other) = $other 
OPTIONAL MATCH (me)-[af:FOLLOWS]->(other) 
CALL 
  apoc.do.when(
    af IS NULL, 
    CREATE (me)-[f:FOLLOWS]->(other), 
    DELETE af
  )

But this results in the error但这会导致错误

Neo4jError: Invalid input '>' (line 1, column 169 (offset: 168))

"MATCH (me:User), (other:User) WHERE ID(me) = $me AND ID(other) = $other OPTIONAL MATCH (me)-[af:FOLLOWS]->(other) CALL apoc.do.when(af IS NULL, CREATE (me)-[f:FOLLOWS]->(other), DELETE af)"

The queries (last two arguments) to apoc.do.when() have to be strings, so quote each of them. apoc.do.when()的查询(最后两个参数)必须是字符串,因此请引用它们中的每一个。

Also, in order for each of those queries to use those variables, you need to pass those variables in a parameter map as a 4th argument.此外,为了让每个查询都使用这些变量,您需要将这些变量作为第四个参数传递到参数 map 中。

Each of the conditional queries must RETURN something, otherwise there will be no rows yielded and anything after would be a no-op.每个条件查询都必须返回一些内容,否则将不会产生任何行,并且之后的任何内容都将是空操作。

The call must YIELD value , so that needs to be present, and last, a query cannot end with a procedure call, so you need to RETURN something.调用必须YIELD value ,所以它需要存在,最后,查询不能以过程调用结束,所以你需要返回一些东西。

This one should work, you can adjust it as needed:这个应该可以工作,您可以根据需要进行调整:

MATCH (me:User), (other:User) 
  WHERE ID(me) = $me AND ID(other) = $other 
OPTIONAL MATCH (me)-[af:FOLLOWS]->(other) 
CALL 
  apoc.do.when(
    af IS NULL, 
    "CREATE (me)-[f:FOLLOWS]->(other) RETURN f", 
    "DELETE af RETURN null as f",
    {me:me, af:af}
  ) YIELD value
RETURN value.f as f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM