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 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
}
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 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
}
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 n.id = $id return n",
map[string]interface{}{
"id": 1237892
})