Neo4j-列表字符串转数字列表

Neo4j-列表字符串转数字列表

  • 问题来自一个需求:用load csv导入neo4j时,数字列表变成了字符串,需要把它转回列表状态。

    由于手头没有原始数据,这里用其他数据库进行实验。

  • Neo4j版本:community-4.2

  • 时间:2022-10-21

方案一

  • 模拟一个列表字符串,保存为节点属性

    MATCH (n:IndustryChain{name:'主动元件'}) 
    set n.list = '[1,2,3,4]'
    
  • 尝试去掉字符串中的括号,然后拆分为列表

    MATCH (n:IndustryChain{name:'主动元件'}) 
    with substring(n.list,1,7) as numstr,n
    with split(numstr,',') as nlist, n
    set n.list = nlist
    
  • 将列表中元素转为整型,修改节点属性为新列表

    MATCH (n:IndustryChain{name:'主动元件'}) 
    with [num in n.list | toInteger(num) ] as numlist,n
    set n.list = numlist
    return n.list
    
  • 整合上述步骤

    MATCH (n:IndustryChain{name:'主动元件'}) 
    with substring(n.list,1,7) as numstr,n
    with split(numstr,',') as nlist, n
    with [num in nlist | toInteger(num) ] as numlist,n
    set n.list = numlist
    
  • 由于列表长度不固定,且没找到获取字符串长度的函数,用replace方式去除括号

    MATCH (n:IndustryChain{name:'主动元件'}) 
    with replace(n.list, "[", "") as strL,n
    with replace(n.list, "]", "") as numstr,n
    with split(numstr,',') as nlist, n
    with [num in nlist | toInteger(num) ] as numlist,n
    set n.list = numlist
    

方案二

  • 新命令:

    MATCH (n:IndustryChain{name:'主动元件'}) 
    with replace(n.list, "[", "") as strL,n
    with replace(n.list, "]", "") as numstr,n
    with split(numstr,',') as nlist, n
    with [num in nlist | toInteger(num) ] as numlist,n
    set n.list = numlist
    

相关知识:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值