Database Reference
In-Depth Information
Listing 9-3. Creating a Node
from py2neo import neo4j, ogm, node, rel
# set connection information (defaults to: http://localhost:7474/db/data/)
graph_db = neo4j.GraphDatabaseService("https://user:password@graphstory.com:7473/db/data/")
# simple method to create node.
user, = graph_db.create({"name": "Greg", 'business': 'Graph Story'})
The create method will always return a list, even when only creating a single node or relationship. Add a
trailing comma to automatically unpack a list containing a single node, as shown in the example.
Warning
Retrieving and Updating a Node
Once nodes have been added to the database, you will need a way to retrieve and modify them. Listing 9-4 shows the
process for finding a node by its node id value and updating it.
Listing 9-4. Retrieving and Updating a Node
from py2neo import neo4j, ogm, node, rel
# set connection information (defaults to: http://localhost:7474/db/data/)
graph_db = neo4j.GraphDatabaseService()
# find the user node by it's node id. In this example, nodeId of 1
userNode = graph_db.node(1)
# update a property - this replaces all existing properties with properties provided.
userNode.set_properties({"business " : "Graph Story" })
Removing a Node
Once a node's graph id has been set and saved into the database, it becomes eligible to be removed when necessary.
To remove a node, set a variable as a node object instance and then call the delete method for the node (Listing 9-5).
You cannot delete any node that is currently set as the start point or end point of any relationship. You must
remove the relationship before you can delete the node.
Note
 
 
Search WWH ::




Custom Search