简体   繁体   English

如何使用 Cypher 返回节点的所有属性?

[英]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:我知道可以使用通配符 (*) 符号返回 Cypher 查询中的所有引用,例如:

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.但是,结果是具有名为“p”的单个节点“列”的行,可以从中访问属性。 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... )?该特定查询无效,但有没有办法实现相同的结果(没有明确列出所有属性,如 p.title,p.price,p... )?

You can't do this in Cypher yet. 你还不能在 Cypher 中做到这一点。 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:编辑(感谢评论指出):您现在可以从 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.在最新版本的 cypher properties(n)将返回节点的所有键和属性。 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.您可以在密码查询中返回n ,它将返回节点的所有键和属性。 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.您可以使用“as”子句并标识每个属性以及您希望列命名的内容。 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这篇博文很好地展示了在 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 .这与JavaScript 展开运算符非常相似。

I'm new to cypher, but it seems that this returns all the keys for a particular type of node:我是 cypher 的新手,但似乎这会返回特定类型节点的所有键:

MATCH (p:product) RETURN keys(p)

Works in Neo4J 3.0.适用于 Neo4J 3.0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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