1
votes

I have JSON data by which i am creating nodes and relationship between nodes using https://github.com/thingdom/node-neo4j connector.

I have following JSON format

   {
     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).

I am creating two node of following properties :

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

Following is my data insertion query:

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)
      });
 }

Firstly i cleared my neo4j database using following query externally ( using neo4j database UI): Now problem is when i query MATCH (n:nodes_type2) return COUNT(n). Since there are 1000 objects in json it should create 1000 nodes.But the result is coming more than 1000 (around 9000) and keeps on changing as every time when i clear the data and restart the script. When i saw in the results there were multiple nodes of the same UID . Shouldn't merge query handel node match and increment counter . Merge is incrementing the counter but after some number, new node is created with same UID.

2

2 Answers

0
votes

I assue your queries are executed massively in parallel,

make sure to have a unique constraint for : nodes_type1(id) and nodes_type2(id) installed, otherwise MERGE cannot guarantee uniqueness.

Also you should change your query to use parameters instead of literal values

And it should also look like this:

            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

I don't think the id and counter on the relationship make sense in a real use-case but for your test it might be ok

1
votes

Based on your given query, I assume the UUID generated looks to be different on each loop :

1000 loops, 3 queries with 3 different node labels.

Can you count distinct uuids you get from your database, like :

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