为什么我的Cypher查询不起作用?

问题描述 投票:1回答:2

我正在制作一个操作Neo4j数据库的Node.js服务器。现在我有一个管理员和帖子系统,管理员添加帖子。这是我的图表当前的样子:

data model

现在我想要网络互连,所以我想在Admin节点“COLLEAGUE_OF”之间添加一个关系。我写了一个Cypher查询但是我收到了Cypher语法错误。

这是语法:

MERGE (c:Admin) 
MERGE (n:BlogPost {mongoId:{idNeo}}) 
MERGE (b:Admin {mongoAdminId:{idNeoAd}}) 
MERGE(n)-[:ADDED_BY]->(b), (b)-[:COLLUEGE_OF]->(c)

这是执行语法的整个路由:

routes.post('/blogPosts/frontpage/:id/:aid', function (req, res) {
  const id = req.param('id');
  const aid = req.param('aid');

    session
    .run("MERGE (c:Admin) MERGE (n:BlogPost {mongoId:{idNeo}}) MERGE (b:Admin {mongoAdminId:{idNeoAd}}) MERGE(n)-[:ADDED_BY]->(b), (b)-[:COLLUEGE_OF]->(c)", {idNeo: id, idNeoAd: aid})
    .then(function(result) {
      res.status(200).json({"response": "BlogPost added to front page."});
      session.close();
    })
    .catch((error) => {
      res.status(400).json(error);
    });
});
node.js neo4j cypher
2个回答
0
投票

假设:COLLUEGE_OF是您想要的拼写(如果这是英文,您可能需要:COLLEAGUE_OF),此查询应采用现有的图形:管理节点并将它们相互连接:

MATCH (c:Admin)
WITH collect(c) as admins
UNWIND admins as admin1
UNWIND admins as admin2
WITH admin1, admin2
WHERE id(admin1) < id(admin2) // prevents matching to the same pair with swapped order, and matching a node to itself
MERGE (admin1)-[:COLLUEGE_OF]-(admin2)

在向图表添加新博客帖子和管理员时将使用此查询(如果以前已处理过,则不会执行合并):

MERGE (n:BlogPost {mongoId:{idNeo}}) 
MERGE (b:Admin {mongoAdminId:{idNeoAd}}) 
MERGE (n)-[:ADDED_BY]->(b)
WITH b
WHERE NOT (b)-[:COLLUEGE_OF]-()
MATCH (c:Admin)
WHERE c <> b
MERGE (b)-[:COLLUEGE_OF]-(c)

也就是说,如果你真的需要:管理员彼此连接,那么以不同的方式对它进行建模要容易得多:某种类型的所有:管理员连接到的ColleagueGroup节点,如下所示:

// create the single :ColleagueGroup node for all admins:
MERGE (group:ColleagueGroup)
WITH group
MATCH (c:Admin)
MERGE (c)-[:COLLUEGE_OF]->(group)

然后添加一个新的管理员

// add a new :Admin to the group
MERGE (n:BlogPost {mongoId:{idNeo}}) 
MERGE (b:Admin {mongoAdminId:{idNeoAd}})
WITH n, b 
MATCH (group:ColleagueGroup)
MERGE (n)-[:ADDED_BY]->(b)
MERGE (b)-[:COLLUEGE_OF]->(group)

并找到一个单独的同事:管理员节点(将全部:管理员):

MATCH (b:Admin {mongoAdminId:{idNeoAd}})-[:COLLUEGE_OF*2]-(colleague:Admin)
RETURN colleague

0
投票

问题出在查询的最后一行,特别是, (b)-[:COLLUEGE_OF]->(c)语句,它会产生语法错误。只需删除逗号并添加其他合并语句即可。

MERGE (c:Admin) 
MERGE (n:BlogPost {mongoId:{idNeo}}) 
MERGE (b:Admin {mongoAdminId:{idNeoAd}}) 
MERGE(n)-[:ADDED_BY]->(b)
MERGE(b)-[:COLLUEGE_OF]->(c)
© www.soinside.com 2019 - 2024. All rights reserved.