smilebuz
smilebuz

Reputation: 377

How to return all properties of a node with distinct on one property using Cypher

I am new to cypher, and I want to get data after using 'DISTINCT', but I could only get the value of 'DISTINCT' property, for example:

CREATE (n:person {name: "a", age: 22})
CREATE (n:person {name: "a", age: 23})
CREATE (n:person {name: "a", age: 24})

I want to get only one node with label "person" whose name is "a", so I try query like this

MATCH (n:person) RETURN DISTINCT n.name

This only return "a", but I want all properties and values of the node, which is {name: "a", age:22}, what should I do?

Upvotes: 1

Views: 1116

Answers (2)

cybersam
cybersam

Reputation: 67019

To get just one person node with the name "a":

MATCH (n:person {name: "a"})
RETURN n
LIMIT 1;

Upvotes: 0

logisima
logisima

Reputation: 7478

You can try this query :

MATCH (n:person) 
WITH n.name, collect(n) AS persons
RETURN persons[0]

collect is an aggregate function, so in it you will have all the node aggreagted by n.name, and I return the first element.

Upvotes: 0

Related Questions