What Types of Data Are Supported in Memgraph?
Memgraph is a powerful graph database that stores data in the form of graph objects—nodes and relationships. Both nodes and relationships can hold different properties with a variety of data types. In this blog post, I’ll share different data types supported by Memgraph and how to use them.
Node Labels and Relationship Data Types
- Nodes. In Memgraph, nodes can have labels that help to group and categorize the dataset. Labels are strings and each node can have multiple labels or none at all. What’s important is that labels can be modified at any time, allowing for flexible data organization.
For example, here's how to create a node with the Person label.
CREATE {p:Person {name: ‘Alice’, age: 27}}
- Relationships. Unlike nodes, relationships must have exactly one type represented as a string. You cannot change the type of a relationship once you set it during creation.
For example, here's how to create two nodes, Alice and Bob, with a :CONNECTED_TO
relationship between them.
CREATE (a:Person {name: 'Alice'}), (b:Person {name: 'Bob'}), (a)-[:CONNECTED_TO]-(b)
Property Data Types
Nodes and relationships in Memgraph store various properties, similar to key-value pairs found in maps. Property names are represented as text, while their values can be of several different types. Each property can store only one value and cannot have duplicate names within the same graph element. However, you can use the same property names across different graph elements. There are no restrictions on the number of properties a single graph element can have, as long as the values are of supported types.
Here is a table of supported property types in Memgraph.
Data Type | Description |
---|---|
Null | Indicates the absence of value, equivalent to the property not existing |
String | A sequence of characters (text) |
Boolean | A true or false value |
Integer | A whole number |
Float | A real number with floating-point precision |
List | An ordered collection of property values, which can be of any supported type |
Map | A collection of key-value pairs where keys are strings and values can be of any supported type |
Duration | A period of time |
Date | A date represented by year, month, and day |
LocalTime | A time without a timezone |
LocalDateTime | A date and time without a timezone |
ZonedDateTime | A date and time within a specific timezone |
Enum | An enumeration value, predefined and limited to specific values |
Maps in Cypher Query Language
Literal Maps
You can explicitly construct literal maps using key-value pairs in Cypher. Read more about Literal Maps in docs.
RETURN {key: 'Value', listKey: [{inner: 'Map1'}, {inner: 'Map2'}]}
Map Projection
Map projection syntax allows constructs maps from nodes, relationships, other map values, and values with properties. A map projection starts with a variable bound to a graph entity and includes a body of comma-separated map elements enclosed in curly braces.
map_variable {map_element, [, ...n]}
Temporal Types
Memgraph supports several temporal types, each with specific creation and manipulation rules:
Duration
Date
LocalTime
LocalDateTime
ZonedDateTime
Duration
Create durations from strings or maps, storing values in microseconds by calling the function duration()
. Read more about Duration in docs.
For strings, the duration format is: P[nD]T[nH][nM][nS]
Example:
CREATE (:F1Laps {lap: duration("PT2M2.33S")});
Maps can contain the following six fields: day
, hour
, minute
, second
, millisecond
, and microsecond
. Every field can be a double, an int or a mixture of both. Memgraph also supports negative durations.
Example:
CREATE (:F1Laps {lap: duration({minute:2, second:2, microsecond:33})});
Date
Create dates from strings or maps by calling the function date().
For strings, the date format is specified by the ISO 8601: YYYY-MM-DD
or YYYYMMDD
or YYYY-MM
.
Example:
CREATE (:Person {birthday: date("1947-07-30")});
For maps, three fields are available: year
, month
and day
Example:
CREATE (:Person {birthday: date({year:1947, month:7, day:30})});
LocalTime
LocalTime values can be created from strings or maps by calling the function localTime().
For strings, the local time format is specified by the ISO 8601: [T]hh:mm:ss
or [T]hh:mm
or [T]hhmmss
or [T]hhmm
or [T]hh
.
Example:
CREATE (:School {Calculus: localTime("09:15:00")});
For maps, five fields are available: hour
, minute
, second
, millisecond
and microsecond
.
Example:
CREATE (:School {Calculus: localTime({hour:9, minute:15})});
LocalDateTime
LocalDateTime values can be created similarly by calling the function localDateTime().
For strings, the local time format is specified by the ISO 8601: YYYY-MM-DDThh:mm:ss
or YYYY-MM-DDThh:mm
or YYYYMMDDThhmmss
or YYYYMMDDThhmm
or YYYYMMDDThh
.
Example:
CREATE (:Flights {AIR123: localDateTime("2021-10-05T14:15:00")});
For maps the following fields are available: year, month, day, hour, minute, second, millisecond and microsecond.
Example:
CREATE (:Flights {AIR123: localDateTime({year:2021, month:10, day:5, hour:14, minute:15})});
ZonedDateTime
ZonedDateTime values can also be created from strings or maps, with timezone information by calling the datetime() function.
The datetime()
function takes strings that follow the ISO 8601 standard. An ISO 8601-compliant string that stands for a zoned datetime value has two parts: <DateTime><timezone>
.
The first part is defined the same way as LocalDateTime
, and the second part follows one of the given timezone formats:
Z
±hh:mm
±hh:mm[ZoneName]
±hhmm
±hhmm[ZoneName]
±hh
±hh[ZoneName]
[ZoneName]
, whereZoneName
is a timezone name from the IANA timezone database
Example:
CREATE (:Flight {AIR123: datetime("2024-04-21T14:15:00-07:00[America/Los_Angeles]")});
CREATE (:Flight {AIR123: datetime("2021-04-21T14:15:00Z")});
Maps for constructing ZonedDateTime values may have the following fields: year
, month
, day
, hour
, minute
, second
, millisecond
, microsecond
and timezone
.
There are two options for the timezone
field:
string
: timezone name from the IANA timezone databaseint
: offset from UTC (in minutes)
Example:
CREATE (:Flight {AIR123: datetime({year: 2024, month: 4, day: 21, hour: 14, minute: 15, timezone: "America/Los_Angeles"})});
CREATE (:Flight {AIR123: datetime({year: 2021, month: 4, day: 21, hour: 14, minute: 15, timezone: -60})});
Enum
Enums need to be defied before they can be used. They represent a set od predefined values. For example:
CREATE ENUM Status VALUES { Good, Okay, Bad };
To see existing enums:
SHOW ENUMS;
Enums can also be referenced using their literal form in queries:
CREATE (:Machine {status: Status::Good});
CREATE (:Machine {status: Status::Okay});
MATCH (n:Machine) WHERE n.status = Status::Bad RETURN n;
The ToEnum()
function takes string(s) to lookup and return enum values.
RETURN ToEnum("Status", "Good");
RETURN ToEnum("Status::Okay");
Conclusion
Memgraph offers a versatile set of data types that cater to a wide range of data storage and query needs. By understanding and using those data types, you can build efficient and powerful graph-based applications. Whether you’re working with simple text and numbers of complex temporal data and enumerations, Memgraph’s data types provide the flexibility you need to manage your data effectively.