重复的节点创build了neo4j和nodejs

我有我使用https://github.com/thingdom/node-neo4j连接器创build节点和节点之间的关系的JSON数据。

我有以下JSON格式

{ att0:"abcd", att1:"val1", att2:"val2", att3:"val3", att4:"val4", att5:"val5", att6:"val6", att7:"val7", att8:"val8" } .... more like this around 1000 Here att0+att1 gives me unique id after md5 hash (let it be UID1) . and att4 gives me unique id after md5 hash (let it be UID2). and att7 gives me unique id after md5 hash (let it be UID3). 

我正在创build以下属性的两个节点:

  Node 1 : { id: UID1 , att3:"val3" } Node 2 : { id:UID2, att5:"val5", att6:"val6" } Relationship from Node 1 --> Node 2 : { id:UID3, att5:"val8" } 

以下是我的数据插入查询:

 for(i=0; i<1000; i++){ // 1000 objects in json // create UID1,UID2 and UID3 based on above info for each object // and create query string as mentioned below query_string = MERGE (n:nodes_type1 {id:'UID1'}) ON CREATE SET n={ id:'UID1', att3:'val3'},n.count=1 ON MATCH SET n.count = n.count +1 MERGE (m:nodes_type2 {id:'UID2'}) ON CREATE SET m={ id:'UID2', att5:'val5', att6:'val6'},m.count=1 ON MATCH SET m.count = m.count +1 MERGE (n)-[x:relation_type {id:'UID3'} ]->(m) ON CREATE SET x={ att8:'val8', id:'UID3' },x.count=1 ON MATCH SET x.count = x.count +1 return n db.query(query_string, params, function (err, results) { if (err) { console.log(err); throw err; } console.log("Node Created !!! "+ event_val) }); } 

首先我清除我的neo4j数据库使用下面的查询外部(使用neo4j数据库UI):现在问题是当我查询MATCH(n:nodes_type2)返回COUNT(n)。 由于json中有1000个对象,因此它应该创build1000个节点。但是结果会超过1000(9000左右),并且每次清除数据并重新启动脚本时都会不断变化。 当我在结果中看到有相同的UID多个节点。 不应该合并查询handel节点匹配和增量计数器。 合并是增加计数器,但在一些数字后,新的节点创build相同的UID。

我认为你的查询是并行执行的,

请确保安装了: nodes_type1(id)nodes_type2(id)的唯一约束条件,否则MERGE不能保证唯一性。

你也应该改变你的查询使用参数,而不是文字值

它也应该是这样的:

  MERGE (n:nodes_type1 {id:{id1}}) ON CREATE SET n.att3={att3},n.count=1 ON MATCH SET n.count = n.count +1 MERGE (m:nodes_type2 {id:{id2}}) ON CREATE SET m.att5={att5}, m.att6={att6},m.count=1 ON MATCH SET m.count = m.count +1 MERGE (n)-[x:relation_type {id:{id3}} ]->(m) ON CREATE SET x.att8={att8},x.count=1 ON MATCH SET x.count = x.count+1 return n,r,m 

我不认为这个关系中的id和counter可以用在真实的用例中,但是对于你的testing来说可能没问题

基于你给出的查询,我假设生成的UUID在每个循环中看起来不同:

1000个循环,3个不同节点标签的查询。

你能计算你从数据库中得到的独特的uuids吗,比如:

 MATCH (n) RETURN count(DISTINCT n.id)