1 回答
TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
您在查詢中指定的值return是從左到右索引的 0。因此,在您的示例中,由于您僅從MATCH(在本例中定義為n)返回一個(gè)值,因此它將在索引 0 處可用。如錯(cuò)誤消息所示,索引一超出范圍。
//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
}
這可能是訪問節(jié)點(diǎn)上屬性值的慣用方式的基線——而不是嘗試訪問整個(gè)節(jié)點(diǎn)(驅(qū)動(dòng)程序通過將 nodeValue 保留為未導(dǎo)出的結(jié)構(gòu)隱式地阻止)從節(jié)點(diǎn)返回單個(gè)屬性,如上例所示。
與驅(qū)動(dòng)程序一起工作時(shí)需要考慮的其他幾點(diǎn)。Result還公開了一種Get(key string) (interface{}, ok)通過返回值的名稱訪問結(jié)果的方法。這樣,如果您需要更改結(jié)果的順序,您的值提取代碼將不會(huì)在嘗試訪問錯(cuò)誤索引時(shí)中斷。所以采取以上內(nèi)容并稍微修改一下:
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]interface{}可用于將值作為參數(shù)傳遞給查詢。
session.Run("match(n) where n.id = $id return n",
map[string]interface{}{
"id": 1237892
})
- 1 回答
- 0 關(guān)注
- 186 瀏覽
添加回答
舉報(bào)
