How to get neo4j id as a return when I import node by python?

How to get neo4j id of these 2 nodes by python ?
I want to show result list. name and neo4j id.

======python code====================
from neo4j import GraphDatabase

uri = "neo4j+s://abc"
driver = GraphDatabase.driver(uri, auth=("neo4j", "abc"))

text="CREATE (a:A{name:ABC}),(b:B{name:BCD}) RETURN a,b"

def GraphImport(tx):
tx.run(text)
with driver.session() as session:
session.write_transaction(GraphImport)

tx.run will return your results. Extract what you want and return it in your GraphImport function.

Here is an example:

https://neo4j.com/docs/api/python-driver/current/api.html#managed-transactions-transaction-functions

1 Like

Thanks :slightly_smiling_face:

How to get only neo4j id by "result.value()" ?

Code:
from neo4j import GraphDatabase

uri = "neo4j+s://abc"
driver = GraphDatabase.driver(uri, auth=("neo4j", "abc"))

text="CREATE (a:A{name:ABC}),(b:B{name:BCD}) RETURN a,b"

def GraphImport(tx):
result =tx.run(text)
print(result.value())

with driver.session() as session:
session.write_transaction(GraphImport)

Result:
<Node element_id='4:e~~~7:41' labels=frozenset({'A'}) properties={'name': 'ABC'}>

You are returning the entire node. As you can see, there are properties of the node you can extract from the result. You see an element_id. This is the new unique identifier. You will notice that it ends with colon and a number. My observation has been that the number is the same as the node’s id that has been deprecated.

You can also chose to return just the node’s id instead of the entire node by replacing your return statement with:

Return id(a), id(b)
1 Like

If your DMBS is new enough, I recommend using elementId over id.

In either case, please read the documentation and be aware of the limitations both have. They should not be used to re-identify a node or relationship across multiple transactions.

Docs elementId:
https://neo4j.com/docs/cypher-manual/5/functions/scalar/#functions-elementid

Docs id:
https://neo4j.com/docs/cypher-manual/5/functions/scalar/#functions-id

1 Like

Thank you very much !! I have achieved my purpose.

Thank you very much !! I have achieved my purpose!!