golang mgo得到空物体 [英] golang mgo getting empty objects

查看:157
本文介绍了golang mgo得到空物体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习go api的发展。我有一个运行在docker容器中的mongodb实例。我试图遵循一些指南,但在简单查询上失败。我不完全理解这里使用bson和json标签。我知道这些术语的含义。所以这是我的代码。

  import(
fmt
time

gopkg.in/mgo.v2/bson


const(
hosts =localhost:27017
database =my_database
username =dev1
password =password123
collection =users


类型用户struct {
user string `bson:userjson:user`
data string
}

func main(){

fmt.Println(Starting应用程序!)

info:=& mgo.DialInfo {
Addrs:[] string {hosts},
超时:60 * time.Second,
数据库:数据库,
用户名:用户名,
密码:密码,
}
$ b $会话,err1:= mgo.DialWithInfo(信息)
如果err1 != nil {
panic(err1)
}
defer session.Close()

col:= session.DB(database).C(collection)

var用户用户
var books [ ]用户
var username =cat

col.Insert(& users {user:dog,data:blah})
err3:= col。 Find(bson.M {user:username})。(& user)
fmt.Println(user)
fmt.Println(err3)
count,err2:= col .Count()
if err2!= nil {
panic(err2)
}
fmt.Println(fmt.Sprintf(Messages count:%d,count))所有(&书籍)
fmt.Println(书籍)
}
$ b fmt.Println(用户)
col.Find(bson.M {})

基本上,我在打印行上得到空对象,但得到正确的消息数。如果有帮助,我会插入robomongo的对象。



解决方案

您必须导出结构的字段,否则它们将被 mgo 包忽略。将用户的字段更改为用户数据

 类型用户struct {
用户字符串`bson:userjson:user`
数据字符串`bson:datajson:data`
}

从MongoDB转换/存储/检索struct值时,将使用字段名称。如果您想使用不同的名称,则可以使用标签,以告知字段映射到的名称。


I'm trying to learn go api development. I have a mongodb instance running in a docker container. I'm trying to follow a few guides but am failing on simple queries. I don't fully understand the use of bson and json tags here. I do know what those terms mean. So here is my code.

import (
    "fmt"
    "time"

    "gopkg.in/mgo.v2/bson"
)

const (
    hosts      = "localhost:27017"
    database   = "my_database"
    username   = "dev1"
    password   = "password123"
    collection = "users"
)

type users struct {
    user string `bson:"user" json:"user"`
    data string
}

func main() {

    fmt.Println("Starting Application!")

    info := &mgo.DialInfo{
        Addrs:    []string{hosts},
        Timeout:  60 * time.Second,
        Database: database,
        Username: username,
        Password: password,
    }

    session, err1 := mgo.DialWithInfo(info)
    if err1 != nil {
        panic(err1)
    }
    defer session.Close()

    col := session.DB(database).C(collection)

    var user users
    var books []users
    var username = "cat"

    col.Insert(&users{user: "dog", data: "blah"})
    err3 := col.Find(bson.M{"user": username}).One(&user)
    fmt.Println(user)
    fmt.Println(err3)
    count, err2 := col.Count()
    if err2 != nil {
        panic(err2)
    }
    fmt.Println(fmt.Sprintf("Messages count: %d", count))

    fmt.Println(user)
    col.Find(bson.M{}).All(&books)
    fmt.Println(books)
}

Basically I'm getting empty objects on the print line but am getting the correct Message count. I inserted the objects with robomongo if that helps.

解决方案

You must export fields of structs, else they are ignored by the mgo package. Change fields of users to User and Data.

type users struct {
    User string `bson:"user" json:"user"`
    Data string `bson:"data" json:"data"` 
}

By default when a struct value is transformed / stored / retrieved from MongoDB, the field name is used. If you want to use different names, you may use tags to tell what names should the fields map to.

这篇关于golang mgo得到空物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆