看来我能成功地连接到我的Mongo数据库了。我能看到我收藏的记录有30张。不过,我是怎么看记录的呢?
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)
}以上输出为: 30 {}{}{{}{}{}{}
发布于 2016-10-27 21:31:01
您需要导出字段名。
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`
}BSON编解码器忽略未导出字段。
https://stackoverflow.com/questions/40294469
复制相似问题