Neo4J:如何设置从字符串到日期的属性?

时间:2018-11-14 09:36:02

标签: date neo4j set cypher

我具有以下具有此属性的节点:

 MATCH (a:Person{name:'Raf', birthDay:'05/07/1992'}),
       (b:Person{name:'Mary', birthDay:'10/08/1991'}),
       (c:Person{name:'Luke', birthDay:'11/01/1995'})

如何将birthDay从String格式设置为Date格式? 以及如何考虑我的生日来排序节点并确定其等级?

1 个答案:

答案 0 :(得分:1)

要做的第一项操作是将生日文本分为不同的部分:

RETURN split('05/07/1992', '/') AS parts

将返回一个数组

["05", "07", "1992"]

第二个操作是创建一个日期对象:

WITH split('05/07/1992', '/') AS parts
RETURN date({day: parts[0], month: parts[1], year: parts[2]})

问题是day, month and year需要整数值,而不是字符串:

WITH [x IN split('05/07/1992', '/') | toInteger(x)] AS parts
RETURN date({day: parts[0], month: parts[1], year: parts[2]})

╒════════════════════════════════════════════════════════╕
│"date({day: parts[0], month: parts[1], year: parts[2]})"│
╞════════════════════════════════════════════════════════╡
│"1992-07-05"                                            │
└────────────────────────────────────────────────────────┘

第二个问题,如何按生日排序节点并确定其等级?

好吧,您可以按时间戳排序,但是,如本文档参考中有关日期类型的内容所示,它没有epochMillis,因此您可以改用datetime。

全流量:

// Create Persons
CREATE (a:Person{name:'Raf', birthDay:'05/07/1992'}),
       (b:Person{name:'Mary', birthDay:'10/08/1991'}),
       (c:Person{name:'Luke', birthDay:'11/01/1995'})

// Set datetime as dob property

MATCH (p:Person)
WITH p, [x IN split(p.birthDay, "/") | toInteger(x)] AS parts
SET p.dob = datetime({day: parts[0], month: parts[1], year: parts[2]})

// Return younger persons

MATCH (p:Person) RETURN p ORDER BY p.dob.epochMillis DESC