Skip to content

Update Parameters section #1278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 86 additions & 45 deletions pages/querying/expressions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: Expressions
description: With Memgraph's rich expression capabilities, advanced computations and data processing are at your fingertips. Explore our extensive documentation and other resources for more.
---
import {CommunityLinks} from '/components/social-card/CommunityLinks'
import { Callout } from 'nextra/components'


# Expressions
Expand Down Expand Up @@ -34,105 +35,145 @@ When automating the queries for Memgraph, it comes in handy to change only some
parts of the query. Usually, these parts are values that are used for filtering
results or similar, while the rest of the query remains the same.

Parameters allow reusing the same query but with different parameter values. The
syntax uses the `$` symbol to designate a parameter name. We don't allow old
Cypher parameter syntax using curly braces. For example, you can parameterize
filtering a node property:
Parameters allow reusing the same query but with different parameter values.
They are indicated using the `$` symbol followed by the parameter name (e.g.,
`$name`, `$limit`).

<Callout type="info">
Memgraph **does not** support the old Cypher syntax using curly braces (e.g.,
`{param}`).
</Callout>

Below are some examples of the usage of parameters in Memgraph.

### Common examples

**Filtering node properties**

```cypher
MATCH (node1 {property: $propertyValue}) RETURN node1;
MATCH (node {property: $propertyValue}) RETURN node;
```

If you want to update a property of a node, you can use the following:
**Updating node properties**

```cypher
MATCH (n) SET n.propertyValue = $propertyNewValue;
```

You can use parameters instead of any literal in the query. For example, if you want to filter nodes based on a specific label, you can execute the following query:
**Using parameters in LIMIT clauses**

Memgraph also supports parameters in `LIMIT` clauses. For example:

```cypher
MATCH (n)
RETURN n
LIMIT $limit;
```

**Using parameters with `LOAD CSV`**

Additionally, Memgraph supports parameters for LOAD CSV queries. For instance:

```cypher
LOAD CSV FROM $file AS row
CREATE (:Node {property: row.property});
```

### Label parameterization

Starting from version 3.1, Memgraph allows node labels to be created using
expressions of type `String` or `List[String]`.

**Static label parameterization**

You can use parameters instead of any literal in the query. For example, if you
want to filter nodes based on a specific label, you can execute the following
query:

```cypher
MATCH (n:$label)
RETURN n;
```

This syntax supports parameters of type `String` and `List[String]`, allowing a
node to have one or multiple labels assigned.

Using parameters as property maps is partially supported, it isn't supported in `MATCH` nor `MERGE`
clause. For example, the following query is illegal:
**Dynamic label parameterization**

You can dynamically assign multiple labels to a node using parameters:

```cypher
MATCH (n $propertyMap) RETURN n;
MATCH (n)
SET n:$labels;
```

but this is supported:
Given the following parameter:

```cypher
CREATE (n $propertyMap) RETURN n;
```json
{
"labels": ["Person", "Student"]
}
```

Memgraph also supports parameters in LIMIT clauses. For example:
### Using property maps

Memgraph supports property maps **in some clauses**. For example:

**Not supported (e.g., in `MATCH` or `MERGE`):**

```cypher
MATCH (n) RETURN n LIMIT $limit;
MATCH (n $propertyMap)
RETURN n;
```

Additionally, Memgraph supports parameters for LOAD CSV queries. For instance:
**Supported (e.g., in `CREATE`)**

```cypher
LOAD CSV FROM $file AS row CREATE (:Node {property: row.property});
CREATE (n $propertyMap)
RETURN n;
```


### Usage with Python driver

To use parameters with a Python driver, use the following syntax:

```python
session.run('CREATE (alice:Person {name: $name, age: $ageValue})',
name='Alice', ageValue=22).consume()
session.run(
'CREATE (alice:Person {name: $name, age: $ageValue})',
name='Alice', ageValue=22
).consume()
```

To do the same with labels:

```python
session.run('CREATE (alice:$label {name: $name, age: $ageValue}',
label='Person', name='Alice', ageValue=22)).consume()
session.run(
'CREATE (alice:$label {name: $name, age: $ageValue}',
label='Person', name='Alice', ageValue=22
).consume()
```

To use a property map as a parameter, you can use the following syntax:

```python
session.run('CREATE (alice:Person $propertyMap)', propertymap={"name": "Alice"}).consume()
session.run(
'CREATE (alice:Person $propertyMap)',
propertymap={"name": "Alice"}
).consume()
```

To use parameters whose names are integers, you will need to wrap parameters in
a dictionary and convert them to strings before running a query:

```python
session.run('CREATE (alice:Person {name: $0, age: $1})',
{'0': "Alice", '1': 22}).consume()
session.run(
'CREATE (alice:Person {name: $0, age: $1})',
{'0': "Alice", '1': 22}
).consume()
```

To use parameters with some other driver, please consult the appropriate
documentation.

## Dynamic node labels creation

Starting from version 3.1, Memgraph allows node labels to be created using
expressions of type `String` or `List[String]`.

For example, the following query will create a node with the label `Foo`:

```
WITH {label: "Foo"} as var
CREATE (:var.label);
```

while the following query will result in the creation of node labels `Foo` and `Bar`.

```
WITH {labels: ["Foo", "Bar"]} as var
CREATE (:var.labels);
```


## CASE

Expand Down