Neo4j日期和日期时间比较问题

3

我是一个有用的助手,可以为您翻译文本。

我遇到了neo4j引擎非常奇怪的行为。

假设我们有一个带有日期和日期时间类型属性的节点:

merge (n:Item {date: date("2019-10-21"), datetime: datetime("2019-10-21") }  )
return n

这些查询运行良好:

match (n:Item)
where n.date>=date("2019-10-20")
return n

match (n:Item)
where n.datetime>=datetime("2019-10-20")
return n

当我尝试比较日期和日期时间(或日期时间和日期)时,空数据集会返回:
match (n:Item)
where n.date>=datetime("2019-10-20")
return n

match (n:Item)
where n.datetime>=date("2019-10-20")
return n

Neo4j版本为3.5.11。

1个回答

3
在neo4j中,你不能直接比较不同类型的时间值(例如,datedatetime)。
  1. With your use case, you could convert the date to a datetime before doing the comparison.

    For example, your last 2 queries could convert the date to a datetime:

    MATCH (n:Item)
    WHERE datetime({date:n.date}) >= datetime("2019-10-20")
    RETURN n;
    
    
    MATCH (n:Item)
    WHERE n.datetime >= datetime({date:date("2019-10-20")})
    RETURN n;
    
  2. But with your use case it would also be valid (and perhaps more efficient) to convert the datetime to a date (notice that is is OK to pass a datetime as the date property value):

    MATCH (n:Item)
    WHERE n.date >= date({date: datetime("2019-10-20")})
    RETURN n;
    
    
    MATCH (n:Item)
    WHERE date({date: n.datetime}) >= date("2019-10-20")
    RETURN n;
    
  3. Yet another approach is to compare the year and ordinalDay values, as in:

    WITH datetime("2019-10-20") AS dt
    MATCH (n:Item)
    WHERE n.date.year >= dt.year AND n.date.ordinalDay >= dt.ordinalDay
    RETURN n;
    
    WITH date("2019-10-20") AS d
    MATCH (n:Item)
    WHERE n.datetime.year >= d.year AND n.datetime.ordinalDay >= d.ordinalDay
    RETURN n;
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接