cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Neo4j Paython Driver

myrights99
Graph Buddy

Hello:

I am new to Neo4j Python Driver, and I am looking for some help on blogs which I am having difficulty finding the right one which shows the code of MERGE creating new nodes with relationships in Python.
Any help with these links is greatly appreciated.

Thanks

1 ACCEPTED SOLUTION

Have a look at the UNWIND clause in Cypher. That might be what you're looking for.

Something along the lines of this maybe?

 

import neo4j


URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "pass")


def work(tx, query, **kwargs):
    return list(tx.run(query, **kwargs))


with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
    with driver.session(database="neo4j") as session:
        nets = [5, 5, 69, 45, 10, 42, 58, 86, 93, 43, 57, 75, 75, 27, 90, 24,
                27, 5, 24, 45, 4]
        result = session.execute_write(
            work,
            "UNWIND $nets AS net "
            "MERGE (n:Network {net: net}) "
            "RETURN n",
            nets=nets
        )
        print(result)

 

View solution in original post

4 REPLIES 4

@myrights99 

https://neo4j.com/docs/python-manual/current/query-simple/#_write_to_the_database  has multiple examples of MERGE and with a single node and with relationships

myrights99
Graph Buddy

Thanks for your reply.  I am using MERGE to create Node along with property as shown below. The problem is that one node is created with all the property values rather than creating that many nodes. I am passing "net" as my dataframe column in MERGE but only one node is created. Not sure what I am missing?

net
5
5
69
45
10
42
58
86
93
43
57
75
75
27
90
24
27
5
24
45
4

 

myrights99
Graph Buddy

As I mentioned above, from below you can see that one node is created instead of multiple nodes. Any help is greatly appreciated.

myrights99_0-1674101848959.png

 

Have a look at the UNWIND clause in Cypher. That might be what you're looking for.

Something along the lines of this maybe?

 

import neo4j


URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "pass")


def work(tx, query, **kwargs):
    return list(tx.run(query, **kwargs))


with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
    with driver.session(database="neo4j") as session:
        nets = [5, 5, 69, 45, 10, 42, 58, 86, 93, 43, 57, 75, 75, 27, 90, 24,
                27, 5, 24, 45, 4]
        result = session.execute_write(
            work,
            "UNWIND $nets AS net "
            "MERGE (n:Network {net: net}) "
            "RETURN n",
            nets=nets
        )
        print(result)