如何解析官方neo4j go驱动程序中的结果?
我在解析when-Cypher查询的结果时遇到问题。With CREATE query like(如README.md上的示例中所示)可以正常工作,但With MATCH不使用结果记录()进行索引。GetByIndex(0)如何解析官方neo4j go驱动程序中的结果?,go,neo4j,Go,Neo4j,我在解析when-Cypher查询的结果时遇到问题。With CREATE query like(如README.md上的示例中所示)可以正常工作,但With MATCH不使用结果记录()进行索引。GetByIndex(0) 由于nodeValue不是导出类型,我不知道hot到断言属性或整个接口返回到nodeValue类型。查询中返回后指定的值从左到右为0索引。因此,在您的示例中,由于您仅从匹配返回一个值(在本例中定义为n),因此它将在索引0处可用。如错误消息所示,索引1超出范围 //in th
由于nodeValue不是导出类型,我不知道hot到断言属性或整个接口返回到nodeValue类型。查询中
返回后指定的值从左到右为0索引。因此,在您的示例中,由于您仅从匹配返回一个值(在本例中定义为n
),因此它将在索引0处可用。如错误消息所示,索引1超出范围
//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 n.id = 1 return n.id, n.name, n.value`, nil)
// index 0 ^ idx 1^ . idx 2^
for result.Next() {
a, ok := result.Record().GetByIndex(0).(int64) //n.id
// ok == true
b, ok := result.Record().GetByIndex(0).(string) //n.name
// ok == true
c, ok := result.Record().GetByIndex(0).(float64)//n.value
// ok == true
}
这可能是访问节点上属性值的惯用方法的一个基准,而不是尝试访问整个节点(驱动程序通过保持nodeValue为未报告的结构隐式阻止了整个节点),从节点返回单个属性,如上面的示例所示
<>在使用驱动程序时要考虑的其他几个方面。code>Result
还公开了一个Get(键字符串)(接口{},ok)
方法,用于通过返回值的名称访问结果。这样,如果您需要更改结果的顺序,那么您的值提取代码在尝试访问错误索引时不会中断。因此,将上述内容稍加修改:
result, _ := session.Run(`
match(n) where n.id = 1 return n.id as nodeId, n.name 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
}
最后要指出的是,map[string]接口{}
可用于将值作为参数传递给查询
session.Run("match(n) where n.id = $id return n",
map[string]interface{}{
"id": 1237892
})
谢谢答案非常详细和清晰!如何将值作为查询中标签的参数传递?我尝试了n:$valueForLabel,但这不起作用。
session.Run("match(n) where n.id = $id return n",
map[string]interface{}{
"id": 1237892
})