Search code examples
cypherredisgraphopencypher

How to return distinct nodes asJson


Using GRAPH.QUERY mykey "MATCH (node:m000) RETURN toJson(node)" I get the following:

[
  {
    "key": "000", "containerid": "10000" // id 1
  },
  {
    "key": "001", "containerid": "10001" // id 2
  },
  {
    "key": "000", "containerid": "10000" // id 3
  }
]

I would like to not get the duplicate item in the results (the last item above).

I tried DISTINCT: GRAPH.QUERY mykey "MATCH (node:m000) RETURN DISTINCT toJson(node)" but that returns all records, presumably because the node id IS unique on each record (even though it isn't in the node properties that are being serialized).

Obviously I can filter the results myself but is there a way to do that in the graph command itself?

Thanks, Murray.


Solution

  • As you said, the id property is unique, thus DISTINCT will not be able to filter out the duplicate without further manual filtering.

    If you don't care about the id of the entity, you can write something like: GRAPH.QUERY mykey MATCH (node:m000) RETURN DISTINCT toJson(properties(node)), toJson(labels(node)), which will filter the duplicate out.