Search code examples
mergeneo4jnodescypher

is there a way to add multiple nodes with properties in Neo4j with Cypher


I'm trying to create a reusable bit of Cypher in which i can quickly add multiple nodes if they aren't already created. This is what I'm attempting:

MERGE (n:Mal_Source {IP:'123.45.543', time:'12.32.12', location:'Canada', Classifier:'IP_ADRESS',Date:'12/21/01'}),
(n:Mal_Source { IP:'123.45.542', time:'12.2.12', location:'Laos', Classifier:'IP_ADRESS',Date:'01/02/13'}),
(n:Mal_Source { IP:'123.45', time:'12.32.1', location:'Uruguay', Classifier:'IP_ADRESS',Date:'03/05/09'}),
(n:Mal_Source { IP:'123.543', time:'1.32.12', location:'Chile', Classifier:'IP_ADRESS',Date:'05/06/07'}),
(n:Mal_Source { IP:'333.45.543', time:'1212.32.12', location:'Vietnam', Classifier:'IP_ADRESS',Date:'06/05/12'}),
(n:Mal_Source { IP:'123.4555.543', time:'5512.32.12', location:'South Korea', Classifier:'IP_ADRESS',Date:'05/09/05'})
RETURN n

I can't figure out why it wont work though.


Solution

  • MERGE checks for pattern so comma seperated nodes doesnt make up a pattern. Individual nodes do make a pattern. MERGE doesnt automatically check for all comma separated nodes.

    2 things you would have to do:

    • Use separate MERGE for all the nodes
    • Name the nodes with different aliases ie n:Mal_Source, m:Mal_Source etc

      MERGE (n:Mal_Source {IP:'123.45.543', time:'12.32.12', location:'Canada',     Classifier:'IP_ADRESS',Date:'12/21/01'})
      MERGE (m:Mal_Source { IP:'123.45.542', time:'12.2.12', location:'Laos', Classifier:'IP_ADRESS',Date:'01/02/13'})
      MERGE (o:Mal_Source { IP:'123.45', time:'12.32.1', location:'Uruguay', Classifier:'IP_ADRESS',Date:'03/05/09'})
      MERGE (p:Mal_Source { IP:'123.543', time:'1.32.12', location:'Chile', Classifier:'IP_ADRESS',Date:'05/06/07'})
      MERGE (q:Mal_Source { IP:'333.45.543', time:'1212.32.12', location:'Vietnam', Classifier:'IP_ADRESS',Date:'06/05/12'})
      MERGE (r:Mal_Source { IP:'123.4555.543', time:'5512.32.12', location:'South Korea', Classifier:'IP_ADRESS',Date:'05/09/05'}) 
      RETURN n,m,o,p,q,r