Star

Created With

linkOn Create Set / On Match Set

QueryBuilderParameters['OnCreateSetI'] / QueryBuilderParameters['OnMatchSetI']

The ON CREATE SET and ON MATCH SET clauses are used with MERGE to specify different actions depending on whether the pattern was created or matched.

linkOn Create Set

Sets properties when a new node is created by a MERGE statement.

linkOn Create Set by using a literal string

A literal string will be used as is.

1linkconst queryBuilder = new QueryBuilder()

2link .merge({

3link identifier: 'n',

4link label: 'Person',

5link properties: { name: 'John' }

6link })

7link .onCreateSet('n.created = timestamp()') /* --> literal string to use */

8link .return('n.name, n.created');

9link

10linkconsole.log(queryBuilder.getStatement());

11link// MERGE (n:Person { name: $name }) ON CREATE SET n.created = timestamp() RETURN n.name, n.created

12link

13linkconsole.log(queryBuilder.getBindParam().get());

14link// { name: 'John' }

linkOn Create Set by using an object

An ON CREATE SET statement can be generated using an object with an identifier and properties.

1linkconst queryBuilder = new QueryBuilder()

2link .merge({

3link identifier: 'n',

4link label: 'User',

5link properties: { email: 'test@example.com' }

6link })

7link .onCreateSet({

8link /* --> identifier whose properties will be set */

9link identifier: 'n',

10link /* --> properties to set when created */

11link properties: {

12link createdAt: '2024-01-01',

13link status: 'pending',

14link },

15link })

16link .return('n');

17link

18linkconsole.log(queryBuilder.getStatement());

19link// MERGE (n:User { email: $email }) ON CREATE SET n.createdAt = $createdAt, n.status = $status RETURN n

20link

21linkconsole.log(queryBuilder.getBindParam().get());

22link// { email: 'test@example.com', createdAt: '2024-01-01', status: 'pending' }

linkOn Create Set with Literal for Cypher functions

You can use the Literal class to include Cypher functions that should not be parameterized.

1linkconst queryBuilder = new QueryBuilder()

2link .merge({

3link identifier: 'n',

4link label: 'Person',

5link properties: { name: 'John' }

6link })

7link .onCreateSet({

8link identifier: 'n',

9link properties: {

10link created: new Literal('timestamp()'),

11link version: 1,

12link },

13link })

14link .return('n');

15link

16linkconsole.log(queryBuilder.getStatement());

17link// MERGE (n:Person { name: $name }) ON CREATE SET n.created = timestamp(), n.version = $version RETURN n

18link

19linkconsole.log(queryBuilder.getBindParam().get());

20link// { name: 'John', version: 1 }

linkOn Match Set

Sets properties when an existing node is matched by a MERGE statement.

linkOn Match Set by using a literal string

A literal string will be used as is.

1linkconst queryBuilder = new QueryBuilder()

2link .merge({

3link identifier: 'n',

4link label: 'Counter',

5link properties: { name: 'pageViews' }

6link })

7link .onMatchSet('n.count = n.count + 1') /* --> literal string to use */

8link .return('n');

9link

10linkconsole.log(queryBuilder.getStatement());

11link// MERGE (n:Counter { name: $name }) ON MATCH SET n.count = n.count + 1 RETURN n

12link

13linkconsole.log(queryBuilder.getBindParam().get());

14link// { name: 'pageViews' }

linkOn Match Set by using an object

An ON MATCH SET statement can be generated using an object with an identifier and properties.

1linkconst queryBuilder = new QueryBuilder()

2link .merge({

3link identifier: 'n',

4link label: 'User',

5link properties: { email: 'test@example.com' }

6link })

7link .onMatchSet({

8link /* --> identifier whose properties will be set */

9link identifier: 'n',

10link /* --> properties to set when matched */

11link properties: {

12link lastLogin: '2024-01-01',

13link loginCount: 5,

14link },

15link })

16link .return('n');

17link

18linkconsole.log(queryBuilder.getStatement());

19link// MERGE (n:User { email: $email }) ON MATCH SET n.lastLogin = $lastLogin, n.loginCount = $loginCount RETURN n

20link

21linkconsole.log(queryBuilder.getBindParam().get());

22link// { email: 'test@example.com', lastLogin: '2024-01-01', loginCount: 5 }

linkOn Match Set with Literal for increment expressions

You can use the Literal class to include expressions like incrementing a counter.

1linkconst queryBuilder = new QueryBuilder()

2link .merge({

3link identifier: 'n',

4link label: 'Counter',

5link properties: { name: 'pageViews' }

6link })

7link .onMatchSet({

8link identifier: 'n',

9link properties: {

10link count: new Literal('n.count + 1'),

11link lastUpdated: new Literal('timestamp()'),

12link },

13link })

14link .return('n');

15link

16linkconsole.log(queryBuilder.getStatement());

17link// MERGE (n:Counter { name: $name }) ON MATCH SET n.count = n.count + 1, n.lastUpdated = timestamp() RETURN n

18link

19linkconsole.log(queryBuilder.getBindParam().get());

20link// { name: 'pageViews' }

linkUsing On Create Set and On Match Set together

You can combine both clauses to handle creation and matching differently.

1linkconst queryBuilder = new QueryBuilder()

2link .merge({

3link identifier: 'n',

4link label: 'Counter',

5link properties: { name: 'pageViews' }

6link })

7link .onCreateSet({

8link identifier: 'n',

9link properties: {

10link count: 1,

11link created: new Literal('timestamp()'),

12link },

13link })

14link .onMatchSet({

15link identifier: 'n',

16link properties: {

17link count: new Literal('n.count + 1'),

18link lastUpdated: new Literal('timestamp()'),

19link },

20link })

21link .return('n');

22link

23linkconsole.log(queryBuilder.getStatement());

24link// MERGE (n:Counter { name: $name }) ON CREATE SET n.count = $count, n.created = timestamp() ON MATCH SET n.count = n.count + 1, n.lastUpdated = timestamp() RETURN n

25link

26linkconsole.log(queryBuilder.getBindParam().get());

27link// { name: 'pageViews', count: 1 }

This pattern is commonly used for:

On Create Set / On Match SetOn Create SetOn Create Set by using a literal stringOn Create Set by using an objectOn Create Set with Literal for Cypher functionsOn Match SetOn Match Set by using a literal stringOn Match Set by using an objectOn Match Set with Literal for increment expressionsUsing On Create Set and On Match Set together

Introduction Getting Started

Modelschevron_right

Sessions and Transactions

Query Builderchevron_right
Query Runnerchevron_right

Bind Parameters

Where Parameters