我正在开发一个使用neo4j数据库的Go项目。我不完全了解如何处理查询的结果。在下面,这是我设法工作的代码,但我想代替.在这里,我有2个返回值,但是如果我有超过10个值要返回怎么办...我不确定,我做得好。return userreturn user.email as email, user.pseudo as pseudodata, 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 })
1 回答
大话西游666
TA贡献1817条经验 获得超14个赞
欢迎!如果查询以 结尾,并且因为看起来需要单个结果,则可以编写:RETURN user
record, err := res.Single()
// [...] check err
userRecord, err := record.Get("user")
// [...] check err
userAttributes := userRecord.(dbtype.Node).Props // 1st cast record into node and extract properties
userData.pseudo := userAttributes["pseudo"].(string) // then cast each property value to expected type
// ...
- 1 回答
- 0 关注
- 78 浏览
添加回答
举报
0/150
提交
取消