Getting results from MATCH when there are no edges


Using a MATCH statement like this returns records from users and folders just fine, but only when there are edges from users to folders:

MATCH {class:Users, as:users, where:(...)}.out("Folders"){as:folders} RETURN $elements

But what syntax would return users (matching the where clause) even if there are no edges to folders?


Hi Eric,

You can use “optional” if you want to also include users that do not have folders:

MATCH {class:Users, as:users, where:(...)}.out("Folders"){as:folders, optional:true} RETURN $elements




Yes, I thought optional:true was the answer, but I can’t get it to work. First off, I’m using 2.2.29, so maybe this is only possible with 3.x?

Here is my full MATCH statment:

MATCH {class:UsersV, as:usersV, where:(...)}.outE('ProE'){where:(enabled = true), optional:true}.inV(){as:proV, where:(enabled = true)}.outE('AppE') RETURN $elements

With this statement (“optional:true” added), I get the following error:
In current MATCH version, optional nodes are allowed only on right terminal nodes, eg. {} --> {optional:true} is allowed, {optional:true} <-- {} is not. DB name="…"

PS - I’ve also tried adding “optional:true” in each of the subsequent functions (although I don’t really care about them–the objective is to return ‘users’ regardless of whether there are any “ProE” outbound edges).


Hi @eric24,

Optional is partially supported also in v 2.2, but only on terminal nodes, ie. not on nodes in the middle of a pattern.
If this is the complete use case, probably you can re-write it with a SELECT / UNWIND as follows:

SELECT @rid as usersV, outE('ProE')[enabled = true].inV() as proV