skip to Main Content

I have created this vertex:

test=# SELECT * FROM cypher('graph', $$
CREATE (s:student{name:"Muneeb", courses: ["OOP", "DS", "Programming", "Android"]})
 RETURN s
$$) as (student agtype);

----------------------------------------------------------------------------------------

     student    
----------------------------------------------------------------------------------------     

{"id": 3377699720527873, "label": "student", "properties": {"name": "Muneeb", "courses": ["OOP", "DS", "Programming", "Android"]}}::vertex
    (1 row)

I want to get the length of courses List. How can I do that.

I tried ARRAY_LENGTH but it didn’t work.

3

Answers


  1. You can use size function.

    SELECT * FROM cypher('graph', $$
         MATCH (u) 
         RETURN size(u.courses)
    $$) as (student agtype);
    

    Result

     student 
    ---------
     4
    (1 row)
    
    Login or Signup to reply.
  2. In addition to taha answer, You can also use jsonb_array_length:
    The example code for this

     SELECT jsonb_array_length((student.properties ->> 'courses')::jsonb) AS 
    
        num_courses
        FROM cypher('graph', $$
            MATCH (s:student)
            WHERE s.name = 'Muneeb'
            RETURN s
        $$) AS (student agtype);
    

    Th output will be:

     num_courses 
    -------------
               4
    (1 row)
    
    Login or Signup to reply.
  3. For this you can use size function
    size() returns the length of a list.

    here is the query
    SELECT * FROM cypher(‘graph_name_here’, $$
    MATCH (u)
    RETURN size(u.name_of_list_you_want_size)
    $$) as (student agtype);

    or

    this is from age documentation link of documentation is official documentation link

     SELECT *
    FROM cypher('graph_name', $$
        RETURN size(['Alice', 'Bob'])
    $$) as (size_of_list agtype);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search