32

I understand it is possible to use the wildcard (*) symbol to return all references in a Cypher query, such as:

MATCH p:Product WHERE p.price='1950' RETURN *;

  ==> +----------------------------------------------------------------+
  ==> | p                                                              |
  ==> +----------------------------------------------------------------+
  ==> | Node[686]{title:"Giorgio Armani Briefcase",price:"1950",...    |
  ==> +----------------------------------------------------------------+

However, the result is a row with a single node 'column' named "p", from which the properties can be accessed. However, I'd like the result-set 'rows' to have the property names as 'columns'. Something like:

MATCH p:Product WHERE p.price='1950' RETURN p.*;

  ==> +-------------------------------------------+
  ==> | title | price | ...                       |
  ==> +-------------------------------------------+
  ==> | "Giorgio Armani Briefcase" | "1950" | ... |
  ==> +-------------------------------------------+

That particular query isn't valid, but is there a way to achieve the same result (short of listing all the properties explicitly, as in p.title,p.price,p... )?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
DavidJ
  • 4,369
  • 4
  • 26
  • 42

7 Answers7

31

You can't do this in Cypher yet. I think it would be a nice feature though, if you want to request it.

Edit (thanks for comment pointing it out): You can now do this as of 2.2:

MATCH (p:Product) WHERE p.price='1950' RETURN keys(p);
Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
Eve Freeman
  • 32,467
  • 4
  • 86
  • 101
  • 1
    Looks like it: https://github.com/neo4j/neo4j/issues/164 and https://trello.com/c/FciCdgWl – Eve Freeman Jul 14 '14 at 19:35
  • 3
    You can get a collection of keys using the `keys` function introduced in 2.2 but there's no way to access a property using a dynamic string key as asked here :( http://stackoverflow.com/questions/29996741/how-to-access-and-mutate-node-property-value-by-the-property-name-string-in-cyph – Matt Byrne Jun 23 '15 at 22:13
  • 7
    Your 2015 update to this answer is confused. `KEYS()` does *not* do what this question was asking for; your original answer remains correct, and your edit only adds confusion. – Mark Amery Aug 10 '16 at 23:18
  • Sorry @MarkAmery -- I see what you mean. (2 years later, :/) – Eve Freeman Oct 01 '18 at 22:02
27

In the latest version of cypher properties(n) will return all the keys and properties of a node. Seems to only work for a single node though.

I hope this helps people.

SStanley
  • 730
  • 8
  • 8
  • This is the actual updated answer since `properties(NODE)` will suit this kind of problem only returning the properties of the node from the query (Without all the node metadata) – Itay Wolfish Jun 21 '22 at 06:44
12

Just to expand on getting the keys:

MATCH (p:product) WITH DISTINCT keys(p) AS keys
UNWIND keys AS keyslisting WITH DISTINCT keyslisting AS allfields
RETURN allfields;
kibblewhite
  • 169
  • 2
  • 6
4

This blog post is a great showcase for manipulating results in Neo4J

If you want to get only the keys, the responses from above are good.

If you want to get only the properties object, without

"identity": 16,
"labels": ["Post"],
"properties": { ... }

You can do:

MATCH p:Product WHERE p.price='1950' RETURN p{.*};

Here is an advanced example involving lists:

MATCH (post:Post)-[:HAS_JOB]-(job)
OPTIONAL MATCH(post)-[:HAS_LIKE]-(like)
OPTIONAL MATCH (post)-[:HAS_USER]-(user)
WITH post, job, user, collect(like{.*}) as likes
RETURN post{
    .*,
    likes: likes,
    job: job{.*},
    user: user{.*}
};

Result example:

{
    "id": "ec704f3b-ce10-4f23-bd06-6d668b7db488",
    "title": "Science Summer"
    "job": {
            "id": "81ae08e4-57d6-4997-9cb8-407e13bc30c6",
            ...
        },
    "user": null,
    "likes": [
        {
            "id": "2209e3a9-701d-4842-9d6b-d4dc8428bac6",
            "name": "alex",
            ...
        }
    ],
}

This is very similar with JavaScript spread operator.

2

You can return n in your cypher query, it will return all the keys and properties of a node. eg.:

MATCH (n:People) n

This will return
n:

{
  "Date_of_Birth": "1981-04-23 00:00:00",
  "Employee_Last_Name": "Aaaa",
  "Employee_First_Name": "Baaa",
  "Age": 36,
  "Employee_Status": "Active"
 }
Ravindra Gupta
  • 1,256
  • 12
  • 42
1

You can use the 'as' clause and identify each property and what you want the column to be named. You will have to identify each property you want returned individually though.

ex:

MATCH p.product where WHERE p.price='1950' RETURN p.price as price, p.title as title, p.whatever, as anythingYouWant
bornytm
  • 793
  • 1
  • 11
  • 27
  • 3
    Thanks for the tip. As it happens, my code is generated so the generator knows the property names statically and I had it generate something like this - though using `p.price? as price` for 'nullable' (absent) properties. – DavidJ Jul 26 '13 at 01:57
0

I'm new to cypher, but it seems that this returns all the keys for a particular type of node:

MATCH (p:product) RETURN keys(p)

Works in Neo4J 3.0.

Laurel
  • 5,965
  • 14
  • 31
  • 57