Carla
Carla

Reputation: 301

Is it possible to update the type of property in Apache AGE?

Suppose I have an Apache AGE graph database 'books' in which some vertices have the pages property stored in different types, as shown in the examples below:

SELECT * FROM cypher('books', $$      
CREATE (v:Book {title: 'A book', pages: 10})
RETURN v $$) as (v agtype);
                                                v                                                 
--------------------------------------------------------------------------------------------------
 {"id": 844424930131969, "label": "Book", "properties": {"pages": 10, "title": "A book"}}::vertex
(1 row)

SELECT * FROM cypher('books', $$
CREATE (v:Book {title: 'Another book', pages: '10'})
RETURN v $$) as (v agtype);
                                                    v                                                     
----------------------------------------------------------------------------------------------------------
 {"id": 844424930131970, "label": "Book", "properties": {"pages": "10", "title": "Another book"}}::vertex
(1 row)

Is there a way to update all occurrences of pages of type string to type integer? In other words, is there a way to update the type of all properties of a particular type?

Upvotes: 1

Views: 255

Answers (2)

Vinay Kumar Talreja
Vinay Kumar Talreja

Reputation: 61

  • You can use typecasting to convert one data type to another using the :: operator.

    SELECT * FROM cypher ('books', 
    $$
      MATCH (v:Book)
      SET v.pages = (v.pages)::integer
      RETURN v
    $$
    ) AS (res agtype);
    

Note: Try to use (v.pages)::int rather than v.pages::int to avoid unexpected results because the :: operator has higher precedence than the . operator.

Upvotes: 0

Mohayu Din
Mohayu Din

Reputation: 399

Yes, It is possible to change the data type of property values for vertices and edges.

Integer To String:

SELECT * FROM cypher('books', $$
  MATCH (v:Book) 
  SET v.pages = tostring(v.pages)
  RETURN v
$$) AS (res agtype);
                                                res                             
                    
----------------------------------------------------------------------------------------------------
 {"id": 844424930131969, "label": "Book", "properties": {"pages": "10", "title": "A book"}}::vertex

String To Integer:

SELECT * FROM cypher('books', $$
  MATCH (v:Book) 
  SET v.pages = (v.pages)::integer
  RETURN n
$$) AS (res agtype);
                                                res                             
                    
--------------------------------------------------------------------------------------------------
 {"id": 844424930131969, "label": "Book", "properties": {"pages": 10, "title": "A book"}}::vertex
(1 row)

You can use typecasting for converting one data type to other using :: operator.

Make sure you use (v.pages)::int rather than v.pages::int to avoid unexpected results because the :: has more precedence than . operator

Upvotes: 2

Related Questions