Skip to content

Instantly share code, notes, and snippets.

@jalakoo
Created April 20, 2025 00:29
Show Gist options
  • Save jalakoo/77a23348a515151babf427a60e3d1e9b to your computer and use it in GitHub Desktop.
Save jalakoo/77a23348a515151babf427a60e3d1e9b to your computer and use it in GitHub Desktop.
Simple Python script for updating all Nodes "name" property in a target Neo4j database
from neo4j import GraphDatabase
import sys
def update_node_names(uri, user, password, target, replace_with):
driver = GraphDatabase.driver(uri, auth=(user, password))
def update_name(tx, target, replace_with):
query = (
"MATCH (n) "
"WHERE n.name = $target "
"SET n.name = $replace_with "
"RETURN n.name"
)
result = tx.run(query, target=target, replace_with=replace_with)
return [record["n.name"] for record in result]
with driver.session() as session:
updated_names = session.write_transaction(update_name, target, replace_with)
print(f"Updated names: {updated_names}")
driver.close()
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python script.py <target_string> <replace_with_string>")
sys.exit(1)
target_string = sys.argv[1]
replace_with_string = sys.argv[2]
# Update these with your Neo4j credentials
NEO4J_URI = "bolt://localhost:7687"
NEO4J_USERNAME = "neo4j"
NEO4J_PASSWORD = "<your_password>"
update_node_names(NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD, target_string, replace_with_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment