简体   繁体   中英

How can I return all properties for a node using Cypher?

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... )?

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);

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.

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;

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"
 }

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM