你如何在 neo4j 中存储时间间隔 属性?

How do you store time interval property in neo4j?

我想知道你是否可以在neo4j中将时间间隔存储为属性,类似于postgresql间隔数据类型。我想存储一些类似个人经历的东西,从几个月到几年不等。如果neo4j中没有这样的数据类型,那么你如何存储这种数据?

您可以使用 Duration 数据类型:

Cypher has built-in support for handling temporal values, and the underlying database supports storing these temporal values as properties on nodes and relationships.

RETURN duration.between(date('2020-01-01'), date.realtime()) AS `experience`

设置属性的示例:

CREATE(p:Person)
SET p.name='John Doe',
    p.startDate = date("2020-01-01");

MATCH(p:Person)
WHERE p.name='John Doe'
    SET p.exp = duration.between(p.startDate, date.realtime())
RETURN p

我不推荐这种方法,因为经验值只有在 created/updated 那天才是准确的。最好使用数据库或应用程序层即时计算这些值,而不是 hard-coding 在数据库中。

上述节点的密码示例为:

MATCH(p:Person)
WHERE p.name='John Doe' 
RETURN p.name AS `Name`,p.startDate as `Start Date`, duration.between(p.startDate, date.realtime()) AS `experience`