重复的节点创建了neo4j和nodejs

Duplicate Nodes created neo4j and nodejs

本文关键字:neo4j nodejs 创建 节点      更新时间:2023-09-26

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

我有以下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).

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

 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数据库UI)清除了我的neo4j数据库:现在的问题是当我查询MATCH(n:nodes_type2)返回COUNT(n)时。由于json中有1000个对象,因此应该创建1000个节点。但结果是超过1000(约9000),并且每次我清除数据并重新启动脚本时都会不断变化。当我在结果中看到有多个节点具有相同的UID时。不应该合并查询句柄节点匹配和增量计数器。Merge是递增计数器,但在某个数字之后,会创建具有相同UID的新节点。

根据您给定的查询,我假设每个循环中生成的UUID看起来不同:

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

你能统计一下你从数据库中得到的不同的uuid吗,比如:

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

我保证您的查询是大规模并行执行的,

请确保为: 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在实际用例中没有意义,但对于您的测试来说,

可能还可以