1 回答

TA貢獻1797條經驗 獲得超6個贊
您在查詢中指定的值return是從左到右索引的 0。因此,在您的示例中,由于您僅從MATCH(在本例中定義為n)返回一個值,因此它將在索引 0 處可用。如錯誤消息所示,索引一超出范圍。
//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 保留為未導出的結構隱式地阻止)從節點返回單個屬性,如上例所示。
與驅動程序一起工作時需要考慮的其他幾點。Result還公開了一種Get(key string) (interface{}, 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]interface{}可用于將值作為參數傳遞給查詢。
session.Run("match(n) where n.id = $id return n",
map[string]interface{}{
"id": 1237892
})
- 1 回答
- 0 關注
- 143 瀏覽
添加回答
舉報