I have a difficulty.
My application uploads a file , and that file uploads the data in Neo4j database. In my Neo4j Application, i already have a database present and the file uploaded also makes its entries in the same database. That is the Database already has an Organisation and it uploads another new Organisation.
So when i do --> Match (n) Return n; --> i get all the data network of both the root - Organisation nodes.
To obtain a single network , i did -
[1]
MATCH (n:Organization) RETURN n; --> ( as Organization is the root node of that uploaded file data)
output - Is only the Organisation single node.
[2]
And with this query --> MATCH (n:Organization)-[r]-(m) RETURN n,r,m
So if I understand correctly, each network has a single root :Organization node, and you want to view only a single network's nodes starting from its root.
You'll need to first match to the :Organization node you want. As you haven't provided any preferences for which one you want, or supplied anything in your query to find a specific :Organization node, we can just match and limit to a single node. From there we can use a variable-length pattern match to get the entire network.
If you're using only outgoing relationships for this hierarchy, then we can ensure we only traverse outgoing relationships:
MATCH (root:Organization)
WITH root
LIMIT 1
MATCH p = (root)-[*]->()
RETURN p
Your query isn't correct. The start of your query doesn't have a label, so it's matching to all nodes:
MATCH (root)
Remember that root here is a variable, and variables are only in scope for the duration of a single query, so even if you used root before in a previous query that doesn't matter, it's out of scope. Your match here is binding a new variable root to every node in your graph.
You need
MATCH (root:Organization)
to ensure that the newly-introduced root variable only binds to nodes with the :Organization label (lookup via label scan).
I did not mention the root node because i want to find the root node. How do we retrieve it ? I want to list down all the root nodes present in my database.
Is an :Organization node always going to be a root node? If so either use the :Organization label when matching to it, or add a secondary :Root label on your :Organization nodes.
If you don't know what type of node the root nodes are going to be, only that they are at the top of any hierarchy, then you can explicitly look for nodes that are not children of any other nodes, then label them as :Root nodes for faster lookup later:
MATCH (n)
WHERE NOT ()-->(n)
SET n:Root
If that doesn't work for you, then you'll need to tell us what the criteria is for a node to be a root node so we can suggest a query to match to them.