Golang Mgo – 如何查看结果?

huangapple go评论124阅读模式
英文:

Golang Mgo - How to view result?

问题

看起来我能够成功连接到我的Mongo数据库。我能够看到我的集合中有30条记录。但是我该如何查看这些记录呢?

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/gorilla/mux"
  6. "gopkg.in/mgo.v2"
  7. "gopkg.in/mgo.v2/bson"
  8. )
  9. type User struct {
  10. id bson.ObjectId `json:"-" bson:"_id"`
  11. firstName string `json:"first_name"`
  12. lastName string `json:"last_name"`
  13. email string `json:"email"`
  14. regId string `json:"registration_id"`
  15. regKey string `json:"registration_key"`
  16. password string `json:"password"`
  17. }
  18. func main() {
  19. session, err := mgo.Dial("XXX.XXX.XXX.XXX")
  20. if err != nil {
  21. panic(err)
  22. }
  23. defer session.Close()
  24. session.SetMode(mgo.Monotonic, true)
  25. //res := []User{}
  26. c := session.DB("cd").C("auth_user")
  27. res := []User{}
  28. fmt.Println(c.Find(bson.M{}).All(&res))
  29. fmt.Println(len(res))
  30. fmt.Println(res)
  31. fmt.Println(res[0])
  32. fmt.Println(res[0].email)
  33. }

以上代码的输出为:

  1. <nil>
  2. 30
  3. [{ } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { }]
英文:

It looks like I'm able to successfully connect to my Mongo Database. I'm able to view that there are 30 records in my collection. How do I actually view the records though?

  1. enter code here
  2. package main
  3. import (
  4. &quot;fmt&quot;
  5. &quot;net/http&quot;
  6. &quot;github.com/gorilla/mux&quot;
  7. &quot;gopkg.in/mgo.v2&quot;
  8. &quot;gopkg.in/mgo.v2/bson&quot;
  9. )
  10. type User struct {
  11. id bson.ObjectId `json:&quot;-&quot; bson:&quot;_id&quot;`
  12. firstName string `json:&quot;first_name&quot;`
  13. lastName string `json:&quot;last_name&quot;`
  14. email string `json:&quot;email&quot;`
  15. regId string `json:&quot;registration_id&quot;`
  16. regKey string `json:&quot;registration_key&quot;`
  17. password string `json:&quot;password`
  18. }
  19. func main() {
  20. session, err := mgo.Dial(&quot;XXX.XXX.XXX.XXX&quot;)
  21. if err != nil {
  22. panic(err)
  23. }
  24. defer session.Close()
  25. session.SetMode(mgo.Monotonic, true)
  26. //res := []User{}
  27. c := session.DB(&quot;cd&quot;).C(&quot;auth_user&quot;)
  28. res := []User{}
  29. fmt.Println(c.Find(bson.M{}).All(&amp;res))
  30. fmt.Println(len(res))
  31. fmt.Println(res)
  32. fmt.Println(res[0])
  33. fmt.Println(res[0].email)
  34. }

The output of the above is:
<nil>
30
[{ } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { }]

答案1

得分: 6

你需要导出字段名。

  1. type User struct {
  2. ID bson.ObjectId `json:"-" bson:"_id"`
  3. FirstName string `json:"first_name"`
  4. LastName string `json:"last_name"`
  5. Email string `json:"email"`
  6. RegId string `json:"registration_id"`
  7. RegKey string `json:"registration_key"`
  8. Password string `json:"password"`
  9. }

BSON编解码器会忽略未导出的字段。

英文:

You need to export the field names.

  1. type User struct {
  2. ID bson.ObjectId `json:&quot;-&quot; bson:&quot;_id&quot;`
  3. FirstName string `json:&quot;first_name&quot;`
  4. LastName string `json:&quot;last_name&quot;`
  5. Email string `json:&quot;email&quot;`
  6. RegId string `json:&quot;registration_id&quot;`
  7. RegKey string `json:&quot;registration_key&quot;`
  8. Password string `json:&quot;password`
  9. }

The BSON codec ignores unexported fields.

huangapple
  • 本文由 发表于 2016年10月28日 05:28:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/40294469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定