map
The map
module offers a versatile toolkit for manipulating collections of
key-value pairs, enabling advanced data operations within a graph database
context.
Trait | Value |
---|---|
Module type | util |
Implementation | C++ |
Graph direction | directed/undirected |
Edge weights | weighted/unweighted |
Parallelism | sequential |
Functions
remove_key()
The procedure removes the specified key and its corresponding value from the input map. If the key does not exist in the input map, it will be ignored. Additionally, when the recursive config is enabled, the key will also be removed from any inner maps that are part of the input map.
Input:
map: Map
➡ The map from which the key will be removed.key: string
➡ The key to be removed from the map.config: Map default = {recursive: false}
➡ The config map which supports therecursive
option. The optionrecursive
isfalse
by default, and should be set totrue
if the input map consists of values that are also maps and, therefore, may have the key to be removed. If anything other thanfalse
ortrue
is placed as a value forrecursive
, it is as iffalse
was placed.
Output:
Map
➡ the map after removing the specified key.
Usage:
The following query will remove the c: "b"
key-value pair from the map:
RETURN map.remove_key({c: "b", d: "ba"}, "c") AS map_without_c;
+----------------------------+
| map_without_c |
+----------------------------+
| {"d": "ba"} |
+----------------------------+
The following query will remove the c: "b"
, c: "h"
and c: "z"
key-value pairs from
the main map and submap due to the recursive
option set to true
:
RETURN map.remove_key({c: "b", d: {e: "ba", c: "h", a: {c: "z"}}}, "c",{recursive: true}) AS removed;
+----------------------------+
| map_without_c |
+----------------------------+
| {"d": {"e": "ba"}} |
+----------------------------+
remove_keys()
The following procedure removes keys from the input map. If recursive option is
set to true
, it will remove keys from maps nested inside the map.
Input:
map: Map[Any]
➡ The input map.keys: List[string]
➡ A list of keys that will be removed.config: Map default = {recursive: false}
➡ A config map which supports therecursive
option. Therecursive
option isfalse
by default, and should be set totrue
if the input map consists of values that are also maps and, therefore, may have the key to be removed.
Output:
Map[Any]
➡ The resulting map.
Usage:
The following query will remove the key: 1
pair.
RETURN map.remove_keys({key: 1, key2:{key: 3, key3: 5}},["key"]) AS result;
+----------------------------------------+
| result |
+----------------------------------------+
|{"key2": {"key": 3,"key3": 5}} |
+----------------------------------------+
The following query will remove the key: 1
and key: 3
pair because te
recursive
options is set to true
.
RETURN map.remove_keys({key: 1, key2:{key: 3, key3: 5}},["key"],{recursive: true}) AS result;
+----------------------------------------+
| result |
+----------------------------------------+
|{"key2": {"key3": 5}} |
+----------------------------------------+
from_pairs()
The procedure creates a map from a list of pairs, where each pair is essentially
another list of size 2. The first element in each pair must be of type string
,
as it will be used as a key in the resulting map.
Input:
pairs: List[List]
➡ The list of pairs.
Output:
Map
➡ A map in which the keys are the first elements in the pairs, and the corresponding values are the second elements in the pairs.
Usage:
The following query will create a map from a list:
RETURN map.from_pairs([["b", 3], ["c", "c"]]) AS map;
+----------------------------+
| map |
+----------------------------+
| {"b": 3, "c": "c"} |
+----------------------------+
merge()
The procedure merges two maps into one. If the same key occurs twice, the later value will overwrite the previous one.
Input:
first: Map
➡ A map containing key-value pairs that need to be merged with another map.second: Map
➡ The second map containing key-value pairs that need to be merged with the key-values from the first map.
Output:
Map
➡ The merged input maps.
Usage:
The following query will merge maps:
RETURN map.merge({a: "b", c: "d"}, {e: "f", g: "h"}) AS merged;
+----------------------------------------+
| merged |
+----------------------------------------+
| {a: "b", c: "d", e: "f", g: "h"} |
+----------------------------------------+
flatten()
The procedure flattens nested items in the input map.
Input:
map: Map[Any]
➡ The input map that needs to be modified.delimiter: string (default = ".")
➡ The delimiter used for flattening.
Output:
Map[Any]
➡ The flattened map, sorted alphabetically by keys.
Usage:
The following query will flatten the contents of the map:
RETURN map.flatten({a: {b:3, d:4}},"/") AS result;
+----------------------------------------+
| result |
+----------------------------------------+
| {"a/b": 3, "a/d": 4} |
+----------------------------------------+
from_lists()
The procedure makes a map from lists of keys and corresponding values.
Input:
keys: List[string]
➡ A list of keys.values
➡ A list of values.
Output:
Map[Any]
➡ The resulting map.
Usage:
The following query will create a map from two lists:
RETURN map.from_lists(["key","key2"],[1,2]) AS result;
+----------------------------------------+
| result |
+----------------------------------------+
| {""key": 1, "key2": 2} |
+----------------------------------------+
from_values()
Returns a map from the given list of values. The list has the format: [key1, value1, key2, value2]
. If the key is not convertible to a string, the function
throws ValueException
.
Input:
values: List[Any]
➡ A list of values.
Output:
Map
➡ The resulting map.
Usage:
The following query will create a map from a list of values:
RETURN map.from_values(["day", "sunny", 5, 6]) AS map;
+----------------------------------------+
| map |
+----------------------------------------+
| {"5": 6, "day": "sunny"} |
+----------------------------------------+
set_key()
Updates the value at the position key
in a map. If the key doesn't exist,
the function will insert it.
Input:
map: Map
➡ The map that will be modified.key: string
➡ The key of a certain key-value pair that needs to have a new value.value: any
➡ The new value of a certain key-value pair.
Output:
Map
➡ The modified map.
Usage:
The following query will replace the value of the key name
:
RETURN map.set_key({name:"Ivan",country:"Croatia"}, "name", "Matija") AS map;
+-------------------------------------------+
| map |
+-------------------------------------------+
| {"country": "Croatia", "name": "Matija"} |
+-------------------------------------------+
Procedures
from_nodes()
Returns a map of all nodes that contain the given label and property. The key
of each map element will be the value of the property (if it is convertible to a
string, otherwise the procedure throws ValueException
).
Input:
label: string
➡ The searched label.property: string
➡ The searched property.
Output:
result: Map
➡ The map containing all the matching nodes, their labels and properties.
Usage:
CREATE (Oppenheimer:Movie {title:'Oppenheimer', released:2023});
CREATE (Barbie:Movie {title:"Barbie", released:2023});
CREATE (Shawshank:Movie {title:'The Shawshank Redemption', released:1994});
CALL map.from_nodes("Movie", "title") YIELD map RETURN map;
{
"Barbie": {
"identity": 53,
"labels": [
"Movie"
],
"properties": {
"released": 2023,
"title": "Barbie"
}
},
"Oppenheimer": {
"identity": 52,
"labels": [
"Movie"
],
"properties": {
"released": 2023,
"title": "Oppenheimer"
}
},
"The Shawshank Redemption": {
"identity": 54,
"labels": [
"Movie"
],
"properties": {
"released": 1994,
"title": "The Shawshank Redemption"
}
}
}