在单个cypher查询nodejs中创建多个不同节点之间的关系

时间:2017-05-03 07:46:48

标签: node.js neo4j cypher loopbackjs

var arr = [{id:"1",created_at:"1",role:"EMPLOYEE"},{id:"2",created_at:"2",role:"EMPLOYEE"},{id:"3",created_at:"3",role:"VISITOR"},{id:"4",created_at:"4",role:"EMPLOYEE"},{id:"5",created_at:"5",role:"FRIEND"},{id:"6",created_at:"6",role:"EMPLOYEE"},{id:"7",created_at:"7",role:"CO_WORKER"},{id:"8",created_at:"8",role:"EMPLOYEE"},{id:"9",created_at:"9",role:"EMPLOYEE"},{id:"10",created_at:"10",role:"STUDENT"}];

var query = 'MATCH (page:pages {id:"435345435"}) ';
for (var i = 0; i < arr.length; i++) {
    query += 'MATCH (user'+i+':users {id:"'+ arr[i].id +'"}) user'+i+' CREATE (user'+i+')-[r'+i+':MEMBER_OF {rId:"'+uuid()+'",relation:"'+  arr[i].role +'",createdAt:"'+ arr[i].created_at +'"} ]->(page) ';
}
query += 'RETURN r0';

给了我 -

MATCH (page:pages {id:"435345435"}) MATCH (user0:users {id:"1"}) CREATE (user0)-[r0:MEMBER_OF {rId:"0",relation:"EMPLOYEE",createdAt:"1"} ]->(page) MATCH (user1:users {id:"2"}) CREATE (user1)-[r1:MEMBER_OF {rId:"1",relation:"EMPLOYEE",createdAt:"2"} ]->(page) MATCH (user2:users {id:"3"}) CREATE (user2)-[r2:MEMBER_OF {rId:"2",relation:"VISITOR",createdAt:"3"} ]->(page) MATCH (user3:users {id:"4"}) CREATE (user3)-[r3:MEMBER_OF {rId:"3",relation:"EMPLOYEE",createdAt:"4"} ]->(page) MATCH (user4:users {id:"5"}) CREATE (user4)-[r4:MEMBER_OF {rId:"4",relation:"FRIEND",createdAt:"5"} ]->(page) MATCH (user5:users {id:"6"}) CREATE (user5)-[r5:MEMBER_OF {rId:"5",relation:"EMPLOYEE",createdAt:"6"} ]->(page) MATCH (user6:users {id:"7"}) CREATE (user6)-[r6:MEMBER_OF {rId:"6",relation:"CO_WORKER",createdAt:"7"} ]->(page) MATCH (user7:users {id:"8"}) CREATE (user7)-[r7:MEMBER_OF {rId:"7",relation:"EMPLOYEE",createdAt:"8"} ]->(page) MATCH (user8:users {id:"9"}) CREATE (user8)-[r8:MEMBER_OF {rId:"8",relation:"EMPLOYEE",createdAt:"9"} ]->(page) MATCH (user9:users {id:"10"}) CREATE (user9)-[r9:MEMBER_OF {rId:"9",relation:"STUDENT",createdAt:"10"} ]->(page) RETURN r0

请建议有效和正确的方法来实现这一目标。 users数组将从REST API

获取

1 个答案:

答案 0 :(得分:1)

您可以将输入数据作为parameterswalk through数组传递给cypher查询,如下所示:

print(each.name)