Search code examples
c#neo4jneo4jclient

How can I get all the property-names of different nodes?


I want to get the effect like using Oracle.

For example,I hava two tables: Student, Tearcher. I can get all the column names of the two tables using SQL.

Now, I have two nodes: one with label Student with Name, age, sex, grade properties, another with label Teacher and Name, sex, salary properties.

Can I get all the property names with Neo4jClient using Cypher?

Thx Z.Tom


Solution

  • You could use the keys function, if you want to get the properties by label you could match accordingly with a query like:

    MATCH (a:Student) WITH keys(a) as k UNWIND k as x RETURN DISTINCT x
    

    Will return all the existing properties on the nodes labeled as Student

    Explanation:

    • The query matches all the nodes labeled as Student.
    • It extracts the property keys of the nodes using keys, then passes them by as arrays WITH
    • UNWIND catches the arrays and splits them into single rows
    • RETURN DISTINCT returns the property names as rows avoiding duplicates

    Example

    On C# using Neo4jClient the code to execute that query would be something like:

    client = new GraphClient(new Uri(connString), dbuser, dbpass);
    client.Connect();
    var results = (Result)client.Cypher.Match("(a:Student)")
                                       .With("keys(a) as k")
                                       .Unwind("k as x")
                                       .ReturnDistinct<Result>("x")
                                       .Results.ToList()
    

    Having a class setup for results like this:

    public class Result
    {
        public string x { get; set; }
    }