Querying All The Neo4j Database Nodes Of Specific Types Using Python


  • Neo4j is a Graph database, which stores information as Property Graphs.
  • In a Property Graph provided by Neo4j both the nodes and the relationships can have labels. The nodes and the relationships can also have properties, which are name value pairs. No relationship that is not connected to a node exists.
  • Graphs present in a Neo4j graph database are created, updated and queried using CQL - the Cypher Query Language.
  • CQL provides a rich syntax to search for various patterns present in the Graphs by specifying nodes, relations and their properties.
  • CQL is also used for creating and updating nodes and relationships in Neo4j.


Querying a Neo4j database nodes from a Python Program:

  • The example Python Program below connects to a Neo4j database server using the URI scheme, which is used to connect to a specific machine hosting Neo4j.
  • A CQL query to search for specific nodes is sent through the Neo4j database session created through the graph database driver.
  • The CQL query matches all the nodes of the graph with the label film.
  • Each node in the query result is printed in the output.


# import the neo4j driver for Python

from neo4j.v1 import GraphDatabase


# Database Credentials

uri             = "bolt://localhost:7687"

userName        = "neo4j"

password        = "test"


# Connect to the neo4j database server

graphDB_Driver  = GraphDatabase.driver(uri, auth=(userName, password))

cql             = "MATCH (x:film) RETURN x"


# Execute the CQL query

with graphDB_Driver.session() as graphDB_Session:

    nodes = graphDB_Session.run(cql)


    for node in nodes:




<Record x=<Node id=0 labels={'film'} properties={'name': 'gone with the wind'}>>

<Record x=<Node id=40 labels={'film'} properties={'name': 'mogambo'}>>


Copyright 2020 © pythontic.com