You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用neo4j-driver存储时间戳被转为Double类型如何解决?

Neo4j数值类型自动转换问题解决方案

你遇到的整数被自动转为Double类型的问题,根源是JavaScript中除法运算默认返回浮点数,该浮点值传入Cypher后会被Neo4j识别为Double类型存储,不是Neo4j主动转换了你的原始整数值。

  • 方案1:显式在Cypher中做整数转换
    你可以直接在Cypher语句中使用toInteger()方法包裹除法运算结果,强制将浮点值转为整数:
datetime({epochSeconds: toInteger(node.createdAt / 1000), nanosecond: 23})
  • 方案2:在Node.js层提前完成整数运算
    不要在Cypher中做除法运算,提前在JS层将计算结果处理为整数再传入,避免Cypher层出现浮点值:
nodes[0].createdAt = 1634717983
// 预处理为秒级整数时间戳
nodes = nodes.map(node => ({
  ...node,
  createdAtSecond: Math.floor(node.createdAt / 1000)
}))

const createUserCyper = `
    UNWIND $nodes as node 
    CREATE (a:Node {
      id: node.id, 
      name: node.name, 
      createdAt: datetime({epochSeconds: node.createdAtSecond, nanosecond: 23})
  })`
const result = await session.run(
  createUserCyper,
  {nodes},
)
  • 额外优化建议
    你当前使用的1634717983本身就是10位的秒级Unix时间戳,不需要做除以1000的运算,直接传入epochSeconds: node.createdAt即可,从根源上避免浮点运算,也就不会出现类型转换问题。

内容的提问来源于stack exchange,提问作者Armen Sanoyan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 00:48:05