我正在开发一个使用neo4j数据库的Go项目。我不完全了解如何处理查询的结果。
在下面,这是我设法工作的代码,但我想代替.在这里,我有2个返回值,但是如果我有超过10个值要返回怎么办...我不确定,我做得好。return userreturn user.email as email, user.pseudo as pseudo
data, err := session.ReadTransaction(func(tx neo4j.Transaction)(interface{}, error) {
res, err := tx.Run(
`match (user:User) where user.email = $email
return user.email as email, user.pseudo as pseudo`,
map[string]interface{}{"email": email})
if err != nil {
return nil, err
}
if res.Next() {
if pseudo, found := res.Record().Get("pseudo"); found {
userData.Pseudo = pseudo.(string)
}
if email, found := res.Record().Get("email"); found {
userData.Email = email.(string)
}
return userData, nil
}
return nil, nil
})
问题是,当我返回而不是,我得到了一个接口,但我不知道如何提取指定id的数据,如.A 给出 .user := res.Record().Values()[0]res.Record().Get("something")user.emailfmt.Printf("%t", user)&{%!t(*types.Node=&{0 [User] map[email:testman@mail.com pseudo:testman]})}
**更新
data, err := session.ReadTransaction(func(tx neo4j.Transaction)(interface{}, error) {
res, err := tx.Run(`MATCH (user:User) WHERE user.email = $email RETURN user`, map[string]interface{}{"email": email})
if err != nil {
return nil, err
}
if res.Next() {
record, err := res.Single()
if err != nil {
fmt.Printf("%s\n", err.Error())
return nil, err
}
userRecord, found := record.Get("user")
if !found {
return nil, errors.New("User not found")
}
userAttributes := userRecord.(map[string]interface{})
userData.Email = userAttributes["email"].(string)
return userData, nil
}
return nil, nil
})
大话西游666
相关分类