DEV Community

Talha Munir ๐Ÿ‡ต๐Ÿ‡ธ
Talha Munir ๐Ÿ‡ต๐Ÿ‡ธ

Posted on

Composite Data types part 2

In part 1 we discussed the composite datatypes part 1. Now in part 2 we will discuss part more on composite datatypes. Our focus would be on Maps.

Maps:

You can construct maps in cypher.
A simple map using an agtypes.
Query:
The below query represents a literal maps with simple Data types.

SELECT *
FROM cypher('graph_name', $$
    WITH {int_key: 1, float_key: 1.0, numeric_key: 1::numeric, bool_key: true, string_key: 'Value'} as m
    RETURN m
$$) AS (m agtype);
Enter fullscreen mode Exit fullscreen mode

It returns the map in the graph.

Maps using Composite Data types:

A map can also contain composite data types e.g. lists and other maps.
Query:

SELECT *
FROM cypher('graph_name', $$
    WITH {listKey: [{inner: 'Map1'}, {inner: 'Map2'}], mapKey: {i: 0}} as m
    RETURN m
$$) AS (m agtype);
Enter fullscreen mode Exit fullscreen mode

The above query will return the list and map on the screen.

You can also access the properties inside the map while querying. For example below query will return the value of the int key which in turn is 1.
Query:

SELECT *
FROM cypher('graph_name', $$
    WITH {int_key: 1, float_key: 1.0, numeric_key: 1::numeric, bool_key: true, string_key: 'Value'} as m
    RETURN m.int_key
$$) AS (int_key agtype);
Enter fullscreen mode Exit fullscreen mode

You can also access list elements in the maps.
Query:
The below query contains a list of maps. You can access the contents of the list using square brackets and an index number in it.

SELECT *
FROM cypher('graph_name', $$
    WITH {listKey: [{inner: 'Map1'}, {inner: 'Map2'}], mapKey: {i: 0}} as m
    RETURN m.listKey[0]
$$) AS (m agtype);
Enter fullscreen mode Exit fullscreen mode

You can also read about lists in the part 1 of the blog here.

References:

You can follow for more content on age website and github.

  1. Apache age
  2. Apache age Github

Top comments (0)