Query modules C++ API

This is the API documentation for mgp.hpp, which contains declarations of all functions in the C++ API for implementing query module procedures and functions. The source file can be found in the Memgraph installation directory, under /usr/include/memgraph.

To see how to implement query modules in C++, take a look at the example we provided.

If you install any C++ modules after running Memgraph, you’ll need to load them into Memgraph or restart Memgraph in order to use them.

Functions and procedures

With this API it’s possible to extend your Cypher queries with functions and procedures with AddProcedure and AddFunction.

The API needs memory access to add procedures and functions, this can be done with mgp::MemoryDispatcherGuard guard(memory);. Old code used mgp::memory = memory;, but this is not thread-safe and has been deprecated. v2.18.1 onwards you should modify your C++ modules and recompile. v2.21 onwards setting mgp::memory will cause a compilation error, so the guard has to be used.

Functions are simple operations that return a single value and can be used in any expression or predicate.

Procedures are more complex computations that may modify the graph, and their output is available to later processing steps in your query. A procedure may only be run from CALL clauses. The output is a stream of records that is made accessible with a YIELD clause.

AddProcedure

Add a procedure to your query module. The procedure is registered as [QUERY_MODULE_NAME].[PROC_NAME] and can be used in Cypher queries.

void AddProcedure(
    mgp_proc_cb callback,
    std::string_view name,
    ProcedureType proc_type,
    std::vector<Parameter> parameters,
    std::vector<Return> returns,
    mgp_module *module,
    mgp_memory *memory);

{

Input:

}

  • callback: procedure callback
  • name: procedure name
  • proc_type: procedure type (read/write)
  • parameters: vector (list) of procedure parameters
  • returns: vector (list) of procedure return values
  • module: the query module that the procedure is added to
  • memory: access to memory

ProcedureType

Enum class for Cypher procedure types.

  • ProcedureType::Read: read procedure
  • ProcedureType::Write: write procedure

AddBatchProcedure

Add a batch procedure to your query module. The procedure is registered as [QUERY_MODULE_NAME].[PROC_NAME] and can be used in Cypher queries.

void AddBatchProcedure(
    mgp_proc_cb callback,
    mgp_proc_initializer initializer,
    mgp_proc_cleanup cleanup,
    std::string_view name,
    ProcedureType proc_type,
    std::vector<Parameter> parameters,
    std::vector<Return> returns,
    mgp_module *module,
    mgp_memory *memory);

{

Input:

}

  • callback: procedure callback, invoked through OpenCypher
  • initializer: procedure initializer, invoked before callback
  • cleanup: procedure cleanup, invoked after batching is done
  • name: procedure name
  • proc_type: procedure type (read/write)
  • parameters: vector (list) of procedure parameters
  • returns: vector (list) of procedure return values
  • module: the query module that the procedure is added to
  • memory: access to memory

ProcedureType

Enum class for Cypher procedure types.

  • ProcedureType::Read: read procedure
  • ProcedureType::Write: write procedure

AddFunction

Add a function to your query module. The function is registered as [QUERY_MODULE_NAME].[FUNC_NAME] and can be used in Cypher queries.

void AddFunction(
    mgp_func_cb callback,
    std::string_view name,
    std::vector<Parameter> parameters,
    std::vector<Return> returns,
    mgp_module *module,
    mgp_memory *memory);

{

Input:

}

  • callback: function callback
  • name: function name
  • parameters: vector (list) of function parameters
  • returns: vector (list) of function return values
  • module: the query module that the procedure is added to
  • memory: access to memory

Parameter

Represents a procedure/function parameter. Parameters are defined by their name, type, and (if optional) default value.

Constructors

Creates a non-optional parameter with the given name and type.

Parameter(std::string_view name, Type type)

Creates an optional Boolean parameter with the given name and default_value.

Parameter(std::string_view name, Type type, bool default_value)

Creates an optional integer parameter with the given name and default_value.

Parameter(std::string_view name, Type type, int default_value)

Creates an optional floating-point parameter with the given name and default_value.

Parameter(std::string_view name, Type type, double default_value)

Creates an optional string parameter with the given name and default_value.

Parameter(std::string_view name, Type type, std::string_view default_value)
Parameter(std::string_view name, Type type, const char *default_value)

Creates a non-optional list parameter with the given name and item_type. The list_type parameter is organized as follows: {Type::List, Type::[ITEM_TYPE]}.

Parameter(std::string_view name, std::pair<Type, Type> list_type)

Creates an optional list parameter with the given name, item_type, and default_value. The list_type parameter is organized as follows: {Type::List, Type::[ITEM_TYPE]}.

Parameter(std::string_view name, std::pair<Type, Type> list_type, Value default_value)

Member variables

NameTypeDescription
namestd::string_viewparameter name
type_Typeparameter type
list_item_type_Type(list parameters) item type
optionalboolwhether the parameter is optional
default_valueValue(optional parameters) default value

Return

Represents a procedure/function return value. Values are defined by their name and type.

Constructors

Creates a return value with the given name and type.

Return(std::string_view name, Type type)

Creates a return value with the given name and list_type. The list_type parameter is organized as follows: {Type::List, Type::[ITEM_TYPE]}.

Return(std::string_view name, std::pair<Type, Type> list_type)

Member variables

NameTypeDescription
namestd::string_viewreturn name
type_Typereturn type
list_item_type_Type(list values) item type

RecordFactory

Factory class for Record.

Constructors

explicit RecordFactory(mgp_result *result)

Member functions

NameDescription
NewRecordAdds a new result record.
SetErrorMessageSets the given error message.
NewRecord

Adds a new result record.

  Record NewRecord() const
SetErrorMessage

Sets the given error message.

  void SetErrorMessage(std::string_view error_msg) const
  void SetErrorMessage(const char *error_msg) const

Record

Represents a record - the building block of Cypher procedure results. Each result is a stream of records, and a function’s record is a sequence of (field name: output value) pairs.

Constructors

explicit Record(mgp_result_record *record)

Member functions

NameDescription
InsertInserts a value of given type under field field_name.
Insert

Inserts a value of given type under field field_name.

  void Insert(const char *field_name, bool value)
  void Insert(const char *field_name, std::int64_t value)
  void Insert(const char *field_name, double value)
  void Insert(const char *field_name, std::string_view value)
  void Insert(const char *field_name, const char *value)
  void Insert(const char *field_name, const List &value)
  void Insert(const char *field_name, const Map &value)
  void Insert(const char *field_name, const Node &value)
  void Insert(const char *field_name, const Relationship &value)
  void Insert(const char *field_name, const Path &value)
  void Insert(const char *field_name, const Date &value)
  void Insert(const char *field_name, const LocalTime value)
  void Insert(const char *field_name, const LocalDateTime value)
  void Insert(const char *field_name, const Duration value)
  void Insert(const char *field_name, const Value &value)

Result

Represents a result - the single return value of a Cypher function.

Constructors

explicit Result(mgp_func_result *result)

Member functions

NameDescription
SetValueSets a return value of given type.
SetErrorMessageSets the given error message.
SetValue

Sets a return value of given type.

  void SetValue(bool value)
  void SetValue(std::int64_t value)
  void SetValue(double value)
  void SetValue(std::string_view value)
  void SetValue(const char *value)
  void SetValue(const List &value)
  void SetValue(const Map &value)
  void SetValue(const Node &value)
  void SetValue(const Relationship &value)
  void SetValue(const Path &value)
  void SetValue(const Date &value)
  void SetValue(const LocalTime &value)
  void SetValue(const LocalDateTime &value)
  void SetValue(const Duration &value)
SetErrorMessage

Sets the given error message.

  void SetErrorMessage(std::string_view error_msg) const
  void SetErrorMessage(const char *error_msg) const

Graph API

This section covers the interface for working with the Memgraph DB graph using the C++ API. A description of data types is available in the reference guide.

Graph

Constructors

explicit Graph(mgp_graph *graph)

Member functions

NameDescription
OrderReturns the graph order (number of nodes).
SizeReturns the graph size (number of relationships).
Nodes (GraphNodes)Returns an iterable structure of the graph’s nodes.
RelationshipsReturns an iterable structure of the graph’s relationships.
GetNodeByIdReturns the graph node with the given ID.
ContainsNodeReturns whether the graph contains the given node (accepts node or its ID).
ContainsRelationshipReturns whether the graph contains the given relationship (accepts relationship or its ID).
IsMutableReturns whether the graph is mutable.
IsTransactionalReturns whether the graph is in a transactional storage mode.
CreateNodeCreates a node and adds it to the graph.
DeleteNodeDeletes a node from the graph.
DetachDeleteNodeDeletes a node and all its incident edges from the graph.
CreateRelationshipCreates a relationship of type type between nodes from and to and adds it to the graph.
DeleteRelationshipDeletes a relationship from the graph.
SetFromChanges the from (start) node of the given relationship.
SetToChanges the to (end) node of the given relationship.
ChangeTypeChanges the relationship type.
Order

Returns the graph order (number of nodes).

int64_t Order() const
Size

Returns the graph size (number of relationships).

int64_t Size() const
Nodes (GraphNodes)

Returns an iterable structure of the graph’s nodes.

GraphNodes Nodes() const
Relationships

Returns an iterable structure of the graph’s relationships.

GraphRelationships Relationships() const
GetNodeById

Returns the graph node with the given ID.

Node GetNodeById(Id node_id) const
ContainsNode

Returns whether the graph contains a node with the given ID.

bool ContainsNode(Id node_id) const

Returns whether the graph contains the given node.

bool ContainsNode(const Node &node) const
ContainsRelationship
bool ContainsRelationship(Id relationship_id) const
bool ContainsRelationship(const Relationship &relationship) const
IsMutable

Returns whether the graph is mutable.

bool IsMutable() const
IsTransactional

Returns whether the graph is in a transactional storage mode.

bool IsTransactional() const
CreateNode

Creates a node and adds it to the graph.

Node CreateNode();
DeleteNode

Deletes a node from the graph.

void DeleteNode(const Node &node)
DetachDeleteNode

Deletes a node and all its incident edges from the graph.

void DetachDeleteNode(const Node &node)
CreateRelationship

Creates a relationship of type type between nodes from and to and adds it to the graph.

Relationship CreateRelationship(const Node &from, const Node &to, std::string_view type)
DeleteRelationship

Deletes a relationship from the graph.

void DeleteRelationship(const Relationship &relationship)
SetFrom

Changes the from (start) node of the given relationship.

void SetFrom(Relationship &relationship, const Node &new_from)
SetTo

Changes the to (end) node of the given relationship.

void SetTo(Relationship &relationship, const Node &set_to)
ChangeType

Changes the relationship type

void ChangeType(Relationship &relationship, std::string_view new_type);

GraphNodes

Auxiliary class providing an iterable view of the nodes contained in the graph. GraphNodes values may only be used for iteration to obtain the values stored within.

Constructors
explicit GraphNodes(mgp_vertices_iterator *nodes_iterator)
Member variables
NameTypeDescription
IteratorGraphNodes::IteratorConst forward iterator for GraphNodes.
Member functions
NameDescription
begin
end
cbegin
cend
Returns the beginning/end of the GraphNodes iterator.

GraphRelationships

Auxiliary class providing an iterable view of the relationships contained in the graph. GraphRelationships values may only be used for iteration to obtain the values stored within.

Constructors
explicit GraphRelationships(mgp_graph *graph)
Member variables
NameTypeDescription
IteratorGraphRelationships::IteratorConst forward iterator for GraphRelationships.
Member functions
NameDescription
begin
end
cbegin
cend
Returns the beginning/end of the GraphRelationship iterator.

Node

Represents a node (vertex) of the Memgraph graph.

Constructors

Creates a Node from the copy of the given mgp_vertex.

explicit Node(mgp_vertex *ptr)
explicit Node(const mgp_vertex *const_ptr)

Copy and move constructors:

Node(const Node &other) noexcept
Node(Node &&other) noexcept

Member functions

NameDescription
IsDeletedReturns whether the node has been deleted.
IdReturns the node’s ID.
LabelsReturns an iterable & indexable structure of the node’s labels.
HasLabelReturns whether the node has the given label.
PropertiesReturns an iterable & indexable structure of the node’s properties.
InRelationshipsReturns an iterable structure of the node’s inbound relationships.
OutRelationshipsReturns an iterable structure of the node’s outbound relationships.
AddLabelAdds a label to the node.
RemoveLabelRemoves a label from the node.
SetPropertySet the value of the node's property.
SetPropertiesUpdate the node's properties.
GetPropertyGet value of node's property
RemovePropertyRemoves the node's property
InDegreeGet the in degree of the node.
OutDegreeGet the out degree of the node.
ToStringReturns the node's string representation.
IsDeleted

Returns whether the node has been deleted.

bool IsDeleted() const
Id

Returns the node’s ID.

mgp::Id Id() const
Labels

Returns an iterable & indexable structure of the node’s labels.

class Labels Labels() const
HasLabel

Returns whether the node has the given label.

bool HasLabel(std::string_view label) const
Properties

Returns an iterable & indexable structure of the node’s properties.

std::unordered_map<std::string, mgp::Value> Properties() const
GetProperty

Gets value of node's property.

mgp::value GetProperty(const std::string& property) const
SetProperty

Sets the value of the node's property.

void SetProperty(std::string key, std::string value)
SetProperties

Updates the node's properties with the given map.

void SetProperties(std::unordered_map<std::string_view, Value> properties)
RemoveProperty

Removes the node's property.

void RemoveProperty(std::string property)
InRelationships

Returns an iterable structure of the node’s inbound relationships.

Relationships InRelationships() const
OutRelationships

Returns an iterable structure of the node’s outbound relationships.

Relationships OutRelationships() const
AddLabel

Adds a label to the node.

void AddLabel(std::string_view label)
RemoveLabel

Removes a label from a node.

void RemoveLabel(std::string_view label)
InDegree

Returns the in degree of a node.

size_t InDegree() const
OutDegree

Returns the out degree of a node.

size_t OutDegree() const
ToString

Returns the node's string representation, which has this format: "(id: node_id, labels: node_labels, properties: node_properties_map)".

std::string ToString() const

Operators

NameDescription
operator[]Returns the value of the node’s property_name property.
operator==
operator!=
operator<
comparison operators
operator[]

Returns the value of the node’s property_name property.

Value operator[](std::string_view property_name) const

Relationship

Represents a relationship (edge) of the Memgraph graph.

Constructors

Creates a Relationship from the copy of the given mgp_edge.

explicit Relationship(mgp_edge *ptr)
explicit Relationship(const mgp_edge *const_ptr)

Copy and move constructors:

Relationship(const Relationship &other) noexcept
Relationship(Relationship &&other) noexcept

Member functions

NameDescription
IsDeletedReturns whether the relationship has been deleted.
IdReturns the relationship’s ID.
TypeReturns the relationship’s type.
PropertiesReturns an iterable & indexable structure of the relationship’s properties.
SetPropertySet the value of the relationship's property.
SetPropertiesUpdate the relationship's properties.
RemovePropertyRemoves the relationship's property.
GetPropertyGet value of relationship's property.
FromReturns the relationship’s source node.
ToReturns the relationship’s destination node.
ToStringReturns the relationship’s string representation.
IsDeleted

Returns whether the relationship has been deleted.

bool IsDeleted() const
Id

Returns the relationship’s ID.

mgp::Id Id() const
Type

Returns the relationship’s type.

std::string_view Type() const
Properties

Returns an iterable & indexable structure of the relationship’s properties.

std::unordered_map<std::string, mgp::Value> Properties() const
GetProperty

Gets value of the relationship's property.

mgp::value GetProperty(const std::string& property) const
SetProperty

Sets the value of the relationship's property.

void SetProperty(std::string key, std::string value)
SetProperties

Updates the relationship's properties with the given map.

void SetProperties(std::unordered_map<std::string_view, Value> properties)
RemoveProperty

Removes the relationship's property.

void RemoveProperty(std::string property)
From

Returns the relationship’s source node.

Node From() const
To

Returns the relationship’s source node.

Node To() const
ToString

Returns the relationship's string representation, which has this format: "(node_from.ToString())-(type: relationship_type, id: relationship_id, properties: relationship_properties_map)->(node_to.ToString())".

std::string ToString() const

Operators

NameDescription
operator[]Returns the value of the relationship’s property_name property.
operator==
operator!=
operator<
comparison operators
operator[]

Returns the value of the relationship’s property_name property.

Value operator[](std::string_view property_name) const

Object is hashable using

std::hash<mgp::Relationship>

Relationships

Auxiliary class providing an iterable view of the relationships adjacent to a node. Relationships values may only be used for iteration to obtain the values stored within.

Constructors
explicit Relationships(mgp_edges_iterator *relationships_iterator)
Member variables
NameTypeDescription
IteratorRelationships::IteratorConst forward iterator for Relationships.
Member functions
NameDescription
begin
end
cbegin
cend
Returns the beginning/end of the Relationships iterator.

Id

Represents the unique ID possessed by all Memgraph nodes and relationships.

Member functions

NameDescription
FromUintConstructs an Id object from uint64_t.
FromIntConstructs an Id object from int64_t.
AsUintReturns the ID value as uint64_t.
AsIntReturns the ID value as int64_t.
FromUint

Constructs an Id object from uint64_t.

static Id FromUint(uint64_t id)
FromInt

Constructs an Id object from int64_t.

static Id FromInt(int64_t id)
AsUint

Returns the ID value as uint64_t.

int64_t AsUint() const
AsInt

Returns the ID value as int64_t.

int64_t AsInt() const

Operators

NameDescription
operator==
operator!=
operator<
comparison operators

Labels

Represents a view of node labels.

Constructors

explicit Labels(mgp_vertex *node_ptr)

Copy and move constructors:

Labels(const Labels &other) noexcept
Labels(Labels &&other) noexcept

Member variables

NameTypeDescription
IteratorLabels::IteratorConst forward iterator for Labels.

Member functions

NameDescription
SizeReturns the number of the labels, i.e. the size of their list.
begin
end
cbegin
cend
Returns the beginning/end of the Labels iterator.
Size

Returns the number of the labels, i.e. the size of their list.

size_t Size() const

Operators

NameDescription
operator[]Returns the node’s label at position index.
operator[]

Returns the node’s label at position index.

std::string_view operator[](size_t index) const

Date

Represents a date with a year, month, and day.

Constructors

Creates a Date object from the copy of the given mgp_date.

explicit Date(mgp_date *ptr)
explicit Date(const mgp_date *const_ptr)

Creates a Date object from the given string representing a date in the ISO 8601 format (YYYY-MM-DD, YYYYMMDD, or YYYY-MM).

explicit Date(std::string_view string)

Creates a Date object with the given year, month, and day properties.

Date(int year, int month, int day)

Copy and move constructors:

Date(const Date &other) noexcept
Date(Date &&other) noexcept

Member functions

NameDescription
NowReturns the current Date.
YearReturns the date’s year property.
MonthReturns the date’s month property.
DayReturns the date’s day property.
TimestampReturns the date’s timestamp (microseconds since Unix epoch).
ToStringReturns the date’s string representation.
Now

Returns the current Date.

static Date Now()
Year

Returns the date’s year property.

int Year() const
Month

Returns the date’s month property.

int Month() const
Day

Returns the date’s day property.

int Day() const
Timestamp

Returns the date’s timestamp (microseconds since Unix epoch).

int64_t Timestamp() const
ToString

Returns the date's string representation, which has this format: "year-month-day".

std::string ToString() const

Operators

NameDescription
operator+
operator-
arithmetic operators
operator==
operator<
comparison operators
operator-
Date operator-(const Duration &dur) const
Duration operator-(const Date &other) const
operator[]

Returns the value of the relationship’s property_name property.

Value operator[](std::string_view property_name) const

Object is hashable using

std::hash<mgp::Date>

LocalTime

Represents a time within the day without timezone information.

Constructors

Creates a LocalTime object from the copy of the given mgp_local_time.

explicit LocalTime(mgp_local_time *ptr)
explicit LocalTime(const mgp_local_time *const_ptr)

Creates a LocalTime object from the given string representing a date in the ISO 8601 format ([T]hh:mm:ss, [T]hh:mm, [T]hhmmss, [T]hhmm, or [T]hh).

explicit LocalTime(std::string_view string)

Creates a LocalTime object with the given hour, minute, second, millisecond, and microsecond properties.

LocalTime(int hour, int minute, int second, int millisecond, int microsecond)

Copy and move constructors:

LocalTime(const LocalTime &other) noexcept
LocalTime(LocalTime &&other) noexcept

Member functions

NameDescription
NowReturns the current LocalTime.
HourReturns the object’s hour property.
MinuteReturns the object’s minute property.
SecondReturns the object’s second property.
MillisecondReturns the object’s millisecond property.
MicrosecondReturns the object’s microsecond property.
TimestampReturns the object’s timestamp (microseconds since Unix epoch).
ToStringReturns the object’s string representation.
Now

Returns the current LocalTime.

static LocalTime Now()
Hour

Returns the object’s hour property.

int Hour() const
Minute

Returns the object’s minute property.

int Minute() const
Second

Returns the object’s second property.

int Second() const
Millisecond

Returns the object’s millisecond property.

int Millisecond() const
Microsecond

Returns the object’s microsecond property.

int Microsecond() const
Timestamp

Returns the object’s timestamp (microseconds since Unix epoch).

int64_t Timestamp() const
ToString

Returns the object's string representation, which has this format: "hour:minute:second,microsecond milisecond".

std::string ToString() const

Operators

NameDescription
operator+
operator-
arithmetic operators
operator==
operator<
comparison operators
operator-
LocalTime operator-(const Duration &dur) const
Duration operator-(const LocalDateTime &other) const

Object is hashable using

std::hash<mgp::LocalTime>

LocalDateTime

Temporal type representing a date and a local time.

Constructors

Creates a LocalDateTime object from the copy of the given mgp_local_date_time.

explicit LocalDateTime(mgp_local_date_time *ptr)
explicit LocalDateTime(const mgp_local_date_time *const_ptr)

Creates a LocalDateTime object from the given string representing a date in the ISO 8601 format (YYYY-MM-DDThh:mm:ss, YYYY-MM-DDThh:mm, YYYYMMDDThhmmss, YYYYMMDDThhmm, or YYYYMMDDThh).

explicit LocalDateTime(std::string_view string)

Creates a LocalDateTime object with the given year, month, day, hour, minute, second, millisecond, and microsecond properties.

LocalDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond)

Copy and move constructors:

LocalDateTime(const LocalDateTime &other) noexcept
LocalDateTime(LocalDateTime &&other) noexcept

Member functions

NameDescription
NowReturns the current LocalDateTime.
YearReturns the object’s year property.
MonthReturns the object’s month property.
DayReturns the object’s day property.
HourReturns the object’s hour property.
MinuteReturns the object’s minute property.
SecondReturns the object’s second property.
MillisecondReturns the object’s millisecond property.
MicrosecondReturns the object’s microsecond property.
TimestampReturns the object’s timestamp (microseconds since Unix epoch).
ToStringReturns the object’s string representation.
Now

Returns the current LocalDateTime.

static LocalDateTime Now()
Year

Returns the object’s year property.

int Year() const
Month

Returns the object’s month property.

int Month() const
Day

Returns the object’s day property.

int Day() const
Hour

Returns the object’s hour property.

int Hour() const
Minute

Returns the object’s minute property.

int Minute() const
Second

Returns the object’s second property.

int Second() const
Millisecond

Returns the object’s millisecond property.

int Millisecond() const
Microsecond

Returns the object’s microsecond property.

int Microsecond() const
Timestamp

Returns the date’s timestamp (microseconds since Unix epoch).

int64_t Timestamp() const
ToString

Returns the object's string representation, which has this format: "year-month-dayThour:minute:second,microsecond milisecond".

std::string ToString() const

Operators

NameDescription
operator+
operator-
arithmetic operators
operator==
operator<
comparison operators
operator-
LocalDateTime operator-(const Duration &dur) const
Duration operator-(const LocalDateTime &other) const

Object is hashable using

std::hash<mgp::LocalDateTime>

Duration

Represents a period of time in Memgraph.

Constructors

Creates a Duration object from the copy of the given mgp_duration.

explicit Duration(mgp_duration *ptr)
explicit Duration(const mgp_duration *const_ptr)

Creates a Duration object from the given string in the following format: P[nD]T[nH][nM][nS], where (1) n stands for a number, (2) capital letters are used as a separator, (3) each field in [] is optional, and (4) only the last field may be a non-integer.

explicit Duration(std::string_view string)

Creates a Duration object from the given number of microseconds.

explicit Duration(int64_t microseconds)

Creates a Duration object with the given day, hour, minute, second, millisecond, and microsecond properties.

Duration(double day, double hour, double minute, double second, double millisecond, double microsecond)

Copy and move constructors:

Duration(const Duration &other) noexcept
Duration(Duration &&other) noexcept

Member functions

NameDescription
MicrosecondsReturns the duration as microseconds.
ToStringReturns the duration's string representation.
Microseconds

Returns the duration as microseconds.

int64_t Microseconds() const
ToString

Returns the duration's string representation, which has this format: "microseconds ms".

std::string ToString() const

Operators

NameDescription
operator+
operator-
arithmetic operators
operator==
operator<
comparison operators
operator-
Duration operator-(const Duration &other) const
Duration operator-() const

Object is hashable using

std::hash<mgp::Duration>

Path

A path is a data structure consisting of alternating nodes and relationships, with the start and end points of a path necessarily being nodes.

Constructors

Creates a Path from the copy of the given mgp_path.

explicit Path(mgp_path *ptr)
explicit Path(const mgp_path *const_ptr)

Creates a Path starting with the given start_node.

explicit Path(const Node &start_node)

Copy and move constructors:

Path(const Path &other) noexcept
Path(Path &&other) noexcept

Member functions

NameDescription
ContainsDeletedReturns whether the path contains any deleted nodes or relationships.
LengthReturns the path length (number of relationships).
GetNodeAtReturns the node at the given index. The index must be less than or equal to length of the path.
GetRelationshipAtReturns the relationship at the given index. The index must be less than length of the path.
ExpandAdds a relationship continuing from the last node on the path.
PopRemoves the last node and the last relationship from the path.
ToStringReturns the path's string representation.
ContainsDeleted

Returns whether the path contains any deleted nodes or relationships.

bool ContainsDeleted() const
Length

Returns the path length (number of relationships).

size_t Length() const
GetNodeAt

Returns the node at the given index. The index must be less than or equal to length of the path.

Node GetNodeAt(size_t index) const
GetRelationshipAt

Returns the relationship at the given index. The index must be less than the length of the path.

Relationship GetRelationshipAt(size_t index) const
Expand

Adds a relationship continuing from the last node on the path.

void Expand(const Relationship &relationship)
Pop

Removes the last node and the last relationship from the path.

void Pop()
ToString

Returns the path's string representation, which has nearly the same format as Relationship.ToString(), the difference being that Path.ToString() can have multiple nodes and relationships in its string representation, for example: "(node)-(relationship)->(node)-(relationship)->(node)...".

std::string ToString() const

Operators

NameDescription
operator==
operator!=
comparison operators

Object is hashable using

std::hash<mgp::Path>

List

A list containing any number of values of any supported type.

Constructors

Creates a List from the copy of the given mgp_list.

explicit List(mgp_list *ptr)
explicit List(const mgp_list *const_ptr)

Creates an empty List.

explicit List()

Creates a List with the given capacity.

explicit List(size_t capacity)

Creates a List from the given vector.

explicit List(const std::vector<Value> &values)
explicit List(std::vector<Value> &&values)

Creates a List from the given initializer_list.

explicit List(std::initializer_list<Value> list)

Copy and move constructors:

List(const List &other) noexcept
List(List &&other) noexcept

Member variables

NameTypeDescription
IteratorList::IteratorConst forward iterator for List containers.

Member functions

NameDescription
ContainsDeletedReturns whether the list contains any deleted values (Node, Relationship, or containers holding them).
SizeReturns the size of the list.
EmptyReturns whether the list is empty.
AppendAppends the given value to the list.
AppendExtendExtends the list and appends the given value to it.
begin
end
cbegin
cend
Returns the beginning/end of the List iterator.
ToStringReturns the list's string representation.
ContainsDeleted

Returns whether the path contains any deleted values (Node, Relationship, or containers holding them).

bool ContainsDeleted() const
Size

Returns the size of the list.

size_t Size() const
Empty

Returns whether the list is empty.

bool Empty() const
Append

Appends the given value to the list. The value is copied.

void Append(const Value &value)

Appends the given value to the list. Takes ownership of value by moving it. The behavior of accessing value after performing this operation is undefined.

void Append(Value &&value)
AppendExtend

Extends the list and appends the given value to it. The value is copied.

void AppendExtend(const Value &value)

Extends the list and appends the given value to it. Takes ownership of value by moving it. The behavior of accessing value after performing this operation is undefined.

void AppendExtend(Value &&value)
ToString

Returns the list's string representation, which has this format: "[element.ToString(), element.ToString()...]".

std::string ToString() const

Operators

NameDescription
operator[]Returns the value at the given index.
operator==
operator!=
comparison operators
operator[]

Returns the value at the given index.

Value operator[](size_t index) const

Object is hashable using

std::hash<mgp::List>

Map

A map of key-value pairs where keys are strings, and values can be of any supported type. The pairs are represented as MapItems.

Constructors

Creates a Map from the copy of the given mgp_map.

explicit Map(mgp_map *ptr)
explicit Map(const mgp_map *const_ptr)

Creates an empty Map.

explicit Map()

Creates a Map from the given STL map.

explicit Map(const std::map<std::string_view, Value> &items)
explicit Map(std::map<std::string_view, Value> &&items)

Creates a Map from the given initializer_list (map items correspond to initializer list pairs).

Map(std::initializer_list<std::pair<std::string_view, Value>> items)

Copy and move constructors:

Map(const Map &other) noexcept
Map(Map &&other) noexcept

Member variables

NameTypeDescription
IteratorList::IteratorConst forward iterator for List containers.

Member functions

NameDescription
ContainsDeletedReturns whether the map contains any deleted values (Node, Relationship, or containers holding them).
SizeReturns the size of the map.
EmptyReturns whether the map is empty.
AtReturns the value at the given key.
InsertInserts the given key-value pair into the map.
UpdateInserts or updates the value at the given key.
EraseErases a mapping by key.
begin
end
cbegin
cend
Returns the beginning/end of the Map iterator.
ToStringReturns the map's string representation.
KeyExistsChecks if the key exists in a map.
ContainsDeleted

Returns whether the path contains any deleted values (Node, Relationship, or containers holding them).

bool ContainsDeleted() const
Size

Returns the size of the map.

size_t Size() const
Empty

Returns whether the map is empty.

bool Empty() const
At

Returns the value at the given key.

Value At(std::string_view key) const
Insert

Inserts the given key-value pair into the map. The value is copied.

void Insert(std::string_view key, const Value &value)

Inserts the given key-value pair into the map. Takes ownership of value by moving it. The behavior of accessing value after performing this operation is undefined.

void Insert(std::string_view key, Value &&value)
Update

Updates the key-value pair in the map. If the key doesn't exist, the value gets inserted. The value is copied.

void Update(std::string_view key, const Value &value)

Updates the key-value pair in the map. If the key doesn't exist, the value gets inserted. The value is copied. Takes the ownership of value by moving it. The behavior of accessing value after performing this operation is undefined.

void Update(std::string_view key, Value &&value)
Erase

Erases the element associated with the key from the map, if it doesn't exist nothing happens.

void Erase(std::string_view key);
ToString

Returns the map's string representation, which has this format: "{key1 : value1.ToString(), key2: value2.ToString()...}".

std::string ToString() const
KeyExists

Returns true if key is present in the map, otherwise false.

bool KeyExists(std::string_view key) const;

Operators

NameDescription
operator[]Returns the value at the given key.
operator==
operator!=
comparison operators
operator[]

Returns the value at the given key.

Value operator[](std::string_view key) const

Object is hashable using

std::hash<mgp::Map>

MapItem

Auxiliary data structure representing key-value pairs where keys are strings, and values can be of any supported type.

Member variables
NameTypeDescription
keystd::string_viewKey for accessing the value stored in a MapItem.
valueValueThe stored value.
Operators
NameDescription
operator==
operator!=
operator<
comparison operators

Object is hashable using

std::hash<mgp::MapItem>

Value

Represents a value of any type supported by Memgraph. The data types are described in the reference guide.

Constructors

Creates a Value from the copy of the given mgp_value.

explicit Value(mgp_value *ptr)

Creates a null Value.

explicit Value()

Basic type constructors:

explicit Value(bool value)
explicit Value(int64_t value)
explicit Value(double value)
explicit Value(const char *value)
explicit Value(std::string_view value)

Container type constructors:

explicit Value(const List &value)
explicit Value(List &&value)
explicit Value(const Map &value)
explicit Value(Map &&value)

Graph element type constructors:

explicit Value(const Node &value)
explicit Value(Node &&value)
explicit Value(const Relationship &value)
explicit Value(Relationship &&value)
explicit Value(const Path &value)
explicit Value(Path &&value)

Temporal type constructors:

explicit Value(const Date &value)
explicit Value(Date &&value)
explicit Value(const LocalTime &value)
explicit Value(LocalTime &&value)
explicit Value(const LocalDateTime &value)
explicit Value(LocalDateTime &&value)
explicit Value(const Duration &value)
explicit Value(Duration &&value)

Copy and move constructors:

Value(const Value &other) noexcept
Value(Value &&other) noexcept

Member functions

NameDescription
ptrReturns the pointer to the stored value.
TypeReturns the type of the value.
Value[TYPE]Returns a value of given type.
Is[TYPE]Returns whether the value is of given type.
ToStringReturns the value's string representation.
Type

Returns the C API pointer to the stored value.

mgp_value *ptr() const
Type

Returns the type of the value, i.e. the type stored in the Value object.

mgp::Type Type() const
Value[TYPE]

Depending on the exact function called, returns a typed value of the appropriate type. Throws an exception if the type stored in the Value object is not compatible with the function called. An overloaded function is available which returns a modifiable (non-const) value of the appropriate type.

bool ValueBool() const
bool ValueBool()
int64_t ValueInt() const
int64_t ValueInt()
double ValueDouble const
double ValueDouble
double ValueNumeric const
double ValueNumeric
std::string_view ValueString() const
std::string_view ValueString()
List ValueList() const
List ValueList()
Map ValueMap() const
Map ValueMap()
Node ValueNode() const
Node ValueNode()
Relationship ValueRelationship() const
Relationship ValueRelationship()
Path ValuePath() const
Path ValuePath()
Date ValueDate() const
Date ValueDate()
LocalTime ValueLocalTime() const
LocalTime ValueLocalTime()
LocalDateTime ValueLocalDateTime() const
LocalDateTime ValueLocalDateTime()
Duration ValueDuration() const
Duration ValueDuration()
Is[TYPE]

Returns whether the value stored in the Value object is of the type in the call.

bool IsNull() const
bool IsBool() const
bool IsInt() const
bool IsDouble() const
bool IsNumeric() const
bool IsString() const
bool IsList() const
bool IsMap() const
bool IsNode() const
bool IsRelationship() const
bool IsPath() const
bool IsDate() const
bool IsLocalTime() const
bool IsLocalDateTime() const
bool IsDuration() const
ToString

Returns the value's string representation. It does this by finding the type of the object wrapped inside the Value object, calling its ToString() function or casting the object to string, depending on it's type. The table below shows the appropriate action for each type.

Data typeString method used
NullReturns ""
NumericCasts numeric type to string.
BoolReturns either "false" or "true", depending on the bool's value.
StringReturns the string.
ListReturns List.ToString().
MapReturns Map.ToString().
NodeReturns Node.ToString().
RelationshipReturns Relationship.ToString().
PathReturns Path.ToString().
DateReturns Date.ToString().
LocalTimeReturns LocalTime.ToString().
LocalDateTimeReturns LocalDateTime.ToString().
DurationReturns Duration.ToString().
std::string ToString() const

Operators

NameDescription
operator==
operator!=
operator<
comparison operators

Object is hashable using

std::hash<mgp::Value>

Additionally, operator << is overloaded for Value and usage of this operator will print the value of the mgp::Value instance (currently doesn't support values of type Path, Map and List).

std::ostream &operator<<(std::ostream &os, const mgp::Value &value)

Type

Enumerates the data types supported by Memgraph and its C++ API. The types are listed and described in the reference guide.

  • Type::Null
  • Type::Any
  • Type::Bool
  • Type::Int
  • Type::Double
  • Type::String
  • Type::List
  • Type::Map
  • Type::Node
  • Type::Relationship
  • Type::Path
  • Type::Date
  • Type::LocalTime
  • Type::LocalDateTime
  • Type::Duration

Additionally, operator<< is overloaded for Type enum, and usage of this operator will print the type represented by mgp::Type enum.

std::ostream &operator<<(std::ostream &os, const mgp::Type &type)

ExecutionHeaders

Represents the headers/columns of the query being executed through the C++ API.

Constructors

ExecutionHeaders(mgp_execution_headers *headers);

This constructor is automatically called during the query execution logic if the user needs headers.

Member functions

NameDescription
SizeReturns the size of the headers/columns.
AtReturns the header at a specific index.
beginReturns an iterator at the beginning position for the headers.
cbeginReturns a const iterator at the beginning position for the headers.
endReturns an iterator at the ending position for the headers.
cendReturns a const iterator at the ending position for the headers.
Size

Returns the size of the headers/columns.

  size_t Size() const
At

Returns the header at a specific index.

  std::string At(size_t index) const
begin

Returns an iterator at the beginning position for the headers.

  Iterator begin()
cbegin

Returns a const iterator at the beginning position for the headers.

  Iterator cbegin()
end

Returns an iterator at the ending position for the headers.

  Iterator end()
cend

Returns a const iterator at the ending position for the headers.

  Iterator cend()

Operators

NameDescription
operator[]Returns a header at a specific index.
operator[]

Returns a header at a specific index.

  std::string_view operator[](size_t index) const

QueryExecution

Represents the object which is able to execute a query through the C++ API.

Constructors

QueryExecution(mgp_graph *graph);

Query execution needs the mgp_graph object because it stores the database context.

Member functions

NameDescription
ExecuteQueryExecutes the query through the C++ API.
ExecuteQuery

Executes the query through the C++ API.

  ExecutionResult ExecuteQuery(std::string_view query, Map params = Map()) const
  ExecutionResult ExecuteQuery(std::string query, Map params = Map()) const

ExecutionResult

Represents the object which is able to handle the pulling logic of the query being executed through the C++ API.

Constructors

ExecutionResult(mgp_execution_result *result, mgp_graph *graph);

The result object is stored in the ExecutionResult to handle the pulling logic. Database context is also needed.

Member functions

NameDescription
HeadersReturns the headers/columns of the executing query.
PullOneReturns one result row from the executing query.
Headers

Returns the headers/columns of the executing query.

  ExecutionHeaders Headers() const
PullOne

Returns one result row from the executing query.

  std::optional<ExecutionRow> PullOne() const

ExecutionRow

Represents a row in the database pulled from the query being executed through the C++ API.

Constructors

ExecutionRow(mgp_map *row);

The mgp_map objects stores the header/column names as keys, and the corresponding values (mgp_value) as map values.

Member functions

NameDescription
SizeReturns the size of the row.
EmptyReturns true if the row has no values.
AtReturns a row value from the given column key.
KeyExistsReturns true if the column exists in a row.
ValuesReturns a map of column keys and row values.
Size

Returns the size of the row.

  size_t Size() const
Empty

Returns true if the row has no values.

  bool Empty() const
At

Returns a row value from the given column key.

  Value At(std::string_view key) const
KeyExists

Returns true if the column exists in a row.

  bool KeyExists(std::string_view key) const
Values

Returns a map of column keys and row values.

  Map Values() const

Operators

NameDescription
operator[]Returns a row value from the given column key.
operator[]

Returns a row value from the given column key.

  Value operator[](std::string_view key) const

Database internals API

This section describes C++ API methods for database operations beyond graph manipulation.

Text search

Text search is an experimental feature introduced in Memgraph 2.15.1. Refer to the text search page for an overview of its capabilities.

To use text search, start memgraph with the --experimental-enabled=text-search flag.

SearchTextIndex

Search the named text index for the given query and get a list of the nodes whose text-indexed properties match the given query.

List SearchTextIndex(
    mgp_graph *memgraph_graph,
    std::string_view index_name,
    std::string_view search_query,
    text_search_mode search_mode);
Input
  • memgraph_graph: the graph
  • index_name: the name of the given text index
  • search_query: the query with which to search the text index
  • search_mode: one of SPECIFIED_PROPERTIES, REGEX, and ALL_PROPERTIES

AggregateOverTextIndex

Aggregate over the results of the search over the named text index and get a JSON-formatted string with the results of the aggregation.

List AggregateOverTextIndex(
    mgp_graph *memgraph_graph,
    std::string_view index_name,
    std::string_view search_query,
    std::string_view aggregation_query);
Input
  • memgraph_graph: the graph
  • index_name: the name of the given text index
  • search_query: the query with which to search the text index
  • aggregation_query: the query (JSON-format) with which to aggregate over search results

Exceptions

During operation, the following exceptions may be thrown.

ExceptionMessage
ValueExceptionvarious (handles unknown/unexpected types)
NotFoundExceptionNode with ID [ID] not found!
NotEnoughMemoryExceptionNot enough memory!
UnknownExceptionUnknown exception!
AllocationExceptionCould not allocate memory!
InsufficientBufferExceptionBuffer is not sufficient to process procedure!
IndexExceptionIndex value out of bounds!
OutOfRangeExceptionIndex out of range!
LogicExceptionLogic exception, check the procedure signature!
DeletedObjectExceptionObject is deleted!
InvalidArgumentExceptionInvalid argument!
InvalidIDExceptionInvalid ID!
KeyAlreadyExistsExceptionKey you are trying to set already exists!
ImmutableObjectExceptionObject you are trying to change is immutable!
ValueConversionExceptionError in value conversion!
SerializationExceptionError in serialization!
TextSearchExceptionvarious (indicates issues with the text search utility)