douyi2107 2018-12-06 18:41
浏览 146

如何解析官方neo4j go驱动程序中的结果?

I have a problem with parsing results from the neo4j-go-driver official Driver when Cypher query is MATCH. With CREATE query like in the example on works fine but with MATCH does not do indexing with result Record().GetByIndex(0)

result, err = session.Run("match(n) where = 1 return n", map[string]interface{}{})
if err != nil {

for result.Next() {
    a := result.Record().GetByIndex(1)         //error: Index out or range
    b := result.Record().GetByIndex(0).(int64) //error: interface {} is *neo4j.nodeValue, not int64
    c := result.Record().GetByIndex(0) //prints corect result: &{14329224 [Item] map[id:1 name:Item 1]}


Since nodeValue is not exported type I do not know hot to asserted attributes or whole interface back to nodeValue type.

  • 写回答

1条回答 默认 最新

  • dougong7850 2018-12-06 21:07

    The values you specify after the return in the query are 0 indexed from left to right. So in your example, since you are only returning one value from the MATCH (in this case defined as n), it will be available at index 0. Index one, as the error message shows, is out of range.

    //in the following example a node has an id of type int64, name of type string, and value of float32
    result, _ := session.Run(`
        match(n) where = 1 return,, n.value`, nil)
                             // index 0 ^  idx 1^ . idx 2^
    for result.Next() {
       a, ok := result.Record().GetByIndex(0).(int64)  //
       // ok == true
       b, ok := result.Record().GetByIndex(0).(string) //
       // ok == true
       c, ok := result.Record().GetByIndex(0).(float64)//n.value
       // ok == true

    This is probably a baseline for an idiomatic way to access property values on a node - instead of trying to access the whole node (which the driver implicitly is discouraging via keeping nodeValue an unexported struct) return individual properties from the node like the above example.

    A few other points to consider when working with the driver. Result also exposes a Get(key string) (interface{}, ok) method for accessing results by the name of the return value. In this way if you need to change the order of your results, your value extraction code won't break trying to access the wrong index. so taking the above and modifying it a little:

    result, _ := session.Run(`
            match(n) where = 1 return as nodeId, as username, n.value as power`, nil)
    for result.Next() {
        record := result.Record()
        nodeID, ok := record.Get("nodeId")
        // ok == true and nodeID is an interface that can be asserted to int
        username, ok := record.Get("username")
        // ok == true and username is an interface that can be asserted to string

    Last thing point out is the map[string]interface{} can be used to pass values as arguments to the query.

    session.Run("match(n) where = $id return n", 
          "id": 1237892
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码