Ask Your Question
1

How can I retrieve a node's properties in Python with Neo4j?

asked 2023-07-04 23:39:08 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-07-04 23:41:01 +0000

lakamha gravatar image

To retrieve a node's properties in Python with Neo4j, you can use the data() method on the node object. Here's an example code snippet:

from neo4j import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))
session = driver.session()

result = session.run("MATCH (n:Person) WHERE n.name = 'John' RETURN n")
node = result.single()["n"]
properties = node.data()

print(properties)

In this example, we first create a connection to the Neo4j database using the GraphDatabase class. Then we open a session using the driver.session() method. We execute a Cypher query to retrieve a node with the name "John", and then we use the result.single() method to retrieve the single record that was returned. We access the node object using the key "n", and then use the data() method to retrieve the node's properties. Finally, we print the properties to the console.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-04 23:39:08 +0000

Seen: 12 times

Last updated: Jul 04 '23