drbuowqe02101 2016-10-27 21:28
浏览 60
已采纳

Golang Mgo-如何查看结果?

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?

enter code here
package main

import (
    "fmt"
    "net/http"

    "github.com/gorilla/mux"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

type User struct {
    id        bson.ObjectId `json:"-" bson:"_id"`
    firstName string        `json:"first_name"`
    lastName  string        `json:"last_name"`
    email     string        `json:"email"`
    regId     string        `json:"registration_id"`
    regKey    string        `json:"registration_key"`
    password  string        `json:"password`
}

func main() {

    session, err := mgo.Dial("XXX.XXX.XXX.XXX")
    if err != nil {
    panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
//res := []User{}
c := session.DB("cd").C("auth_user")

res := []User{}
fmt.Println(c.Find(bson.M{}).All(&res))
fmt.Println(len(res))
fmt.Println(res)
fmt.Println(res[0])
fmt.Println(res[0].email)
}

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

  • 写回答

1条回答 默认 最新

  • douyudouchao6779 2016-10-27 21:31
    关注

    You need to export the field names.

    type User struct {
      ID        bson.ObjectId `json:"-" bson:"_id"`
      FirstName string        `json:"first_name"`
      LastName  string        `json:"last_name"`
      Email     string        `json:"email"`
      RegId     string        `json:"registration_id"`
      RegKey    string        `json:"registration_key"`
      Password  string        `json:"password`
    }
    

    The BSON codec ignores unexported fields.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答