Golang Mgo – 如何查看结果?

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

Golang Mgo - How to view result?

问题

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

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)
}

以上代码的输出为:

<nil>
30
[{      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      } {      }]
英文:

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 (
    &quot;fmt&quot;
	&quot;net/http&quot;

	&quot;github.com/gorilla/mux&quot;
	&quot;gopkg.in/mgo.v2&quot;
	&quot;gopkg.in/mgo.v2/bson&quot;
)

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

func main() {

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

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

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

答案1

得分: 6

你需要导出字段名。

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编解码器会忽略未导出的字段。

英文:

You need to export the field names.

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

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:

确定