Neo4J Cypher - 查找整数属性
Neo4J Cypher - Find integer attributes
背景
我有一些节点将特定属性记录为 LONG,我希望将它们记录为 INT - 我已经修改了系统的输入代码,现在将它们记录为 INT,但是我希望检查 Neo4J 是否存在异常.
数据
我现在混合了数据,其中一些被创建为“int”,一些被创建为 STR,还有一些甚至为空(这在我的数据结构中是可以的)
CREATE (n:logs{value: "example", records: "30"})
, (n:logs{value: "example", records: 30})
, (n:logs{value: "example", records: null})
RETURN n
问题
我试图验证系统现在是否将“记录”属性写入 INT 而不是字符串,所以我尝试了以下查询;
WITH datetime("2021-06-18T06:00:00").epochMillis AS threshold
MATCH(n:logs)
WHERE n.records <> tointeger(n.records)
COUNT(n)
This returns 1 - 我的 NULL 记录,出于某种原因。但不是 STR,正如我所期望的那样。
然后我尝试以下操作(6 月 18 日是我输入系统的更新日期);
WITH datetime("2021-06-18T06:00:00").epochMillis AS threshold
MATCH(n:logs)
WHERE n.records = tointeger(n.records)
COUNT(n)
还有这个 returns 0 - 同样,我不确定为什么。
所以我的问题是,我做错了什么以及我怎样才能到达我需要去的地方。最终是为了;
- 检查系统是否正在保存为 INT 前进
- 将所有预先存在的记录从 STR 更改为 INT。
我假设#2 会是这样的
MATCH(n:logs)
WHERE n.records <> tointeger(n.records)
SET n.records = tointeger(n.records)
附加信息
- 社区版
- 版本X.x.x
- 没有插件
您对更新为整数的查询是正确的。
为了阐明您的假设,请运行在您的 neo4j 桌面或浏览器中查询下方内容,您将看到发生了什么。
1. RETURN toInteger("2021-06-18T06:00:00")
2. RETURN toInteger(null)
它们都将 return NULL。这意味着您的以下说法不正确
This returns 1 - my NULL record, for some reason. But NOT the
STR, as I would have expected.
你做了 return 1 但它是记录的字符串值 ('30') 而不是 NULL 值。
然后当您运行您在下方查询时
WHERE n.records = tointeger(n.records)
And this returns 0 - which again, I am not sure why.
因为字符串日期类型的整数值也是NULL。因此它将return没有匹配的记录。
如果你想计算所有具有非整数属性的节点,包括空值那么你可以运行下面的查询。
MATCH(n:logs)
WHERE n.records <> tointeger(n.records) OR n.records is null
RETURN count(distinct n) as cnt
Result: 2
记住,如果n.records = "2021-06-18T06:00:00" 那么你需要把它转换成纪元值(从1/1/开始的以秒或毫秒为单位的时间整数值1970)。如果不是,则 tointeger(n.records) 为 null 并且在您的查询中不匹配。
执行如下操作:
MATCH (n:logs)
WHERE tointeger(n.records) is null
SET n.records = datetime(n.records).epochMillis
RETURN n
然后执行您的原始查询(这是正确的!),以清除其他非整数值。
MATCH(n:logs)
WHERE n.records <> tointeger(n.records)
SET n.records = tointeger(n.records)
背景
我有一些节点将特定属性记录为 LONG,我希望将它们记录为 INT - 我已经修改了系统的输入代码,现在将它们记录为 INT,但是我希望检查 Neo4J 是否存在异常.
数据
我现在混合了数据,其中一些被创建为“int”,一些被创建为 STR,还有一些甚至为空(这在我的数据结构中是可以的)
CREATE (n:logs{value: "example", records: "30"})
, (n:logs{value: "example", records: 30})
, (n:logs{value: "example", records: null})
RETURN n
问题
我试图验证系统现在是否将“记录”属性写入 INT 而不是字符串,所以我尝试了以下查询;
WITH datetime("2021-06-18T06:00:00").epochMillis AS threshold
MATCH(n:logs)
WHERE n.records <> tointeger(n.records)
COUNT(n)
This returns 1 - 我的 NULL 记录,出于某种原因。但不是 STR,正如我所期望的那样。
然后我尝试以下操作(6 月 18 日是我输入系统的更新日期);
WITH datetime("2021-06-18T06:00:00").epochMillis AS threshold
MATCH(n:logs)
WHERE n.records = tointeger(n.records)
COUNT(n)
还有这个 returns 0 - 同样,我不确定为什么。
所以我的问题是,我做错了什么以及我怎样才能到达我需要去的地方。最终是为了;
- 检查系统是否正在保存为 INT 前进
- 将所有预先存在的记录从 STR 更改为 INT。
我假设#2 会是这样的
MATCH(n:logs)
WHERE n.records <> tointeger(n.records)
SET n.records = tointeger(n.records)
附加信息
- 社区版
- 版本X.x.x
- 没有插件
您对更新为整数的查询是正确的。
为了阐明您的假设,请运行在您的 neo4j 桌面或浏览器中查询下方内容,您将看到发生了什么。
1. RETURN toInteger("2021-06-18T06:00:00")
2. RETURN toInteger(null)
它们都将 return NULL。这意味着您的以下说法不正确
This returns 1 - my NULL record, for some reason. But NOT the STR, as I would have expected.
你做了 return 1 但它是记录的字符串值 ('30') 而不是 NULL 值。
然后当您运行您在下方查询时
WHERE n.records = tointeger(n.records)
And this returns 0 - which again, I am not sure why.
因为字符串日期类型的整数值也是NULL。因此它将return没有匹配的记录。
如果你想计算所有具有非整数属性的节点,包括空值那么你可以运行下面的查询。
MATCH(n:logs)
WHERE n.records <> tointeger(n.records) OR n.records is null
RETURN count(distinct n) as cnt
Result: 2
记住,如果n.records = "2021-06-18T06:00:00" 那么你需要把它转换成纪元值(从1/1/开始的以秒或毫秒为单位的时间整数值1970)。如果不是,则 tointeger(n.records) 为 null 并且在您的查询中不匹配。
执行如下操作:
MATCH (n:logs)
WHERE tointeger(n.records) is null
SET n.records = datetime(n.records).epochMillis
RETURN n
然后执行您的原始查询(这是正确的!),以清除其他非整数值。
MATCH(n:logs)
WHERE n.records <> tointeger(n.records)
SET n.records = tointeger(n.records)