在Go语言中序列化MongoDB数据时出现空属性

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

Empty properties when serializing MongoDB data in Go

问题

这可能是一个非常愚蠢的问题,但是在搜索了两个小时之后,我决定在这里提问。

我尝试学习Go,并编写了一个非常简单的“Hello World”应用程序,其中使用Mongo作为数据源。我可以正常连接,可以获取数据,并且对象的数量也是正确的。

问题是,我将数据映射到的对象的属性为空,尽管Mongo中有数据。

我在Mongo中有一个非常简单的集合,名为"stations",大约有12k条记录,如下所示:

{ "_id" : ObjectId("563c8d56819c3c91076b7c13"), "nm" : "00000BE8" }
{ "_id" : ObjectId("563c8d57819c3c91076b7c1a"), "nm" : "00000C01" }
{ "_id" : ObjectId("563c8d58819c3c91076b7c1d"), "nm" : "00000C02" }
{ "_id" : ObjectId("563c8d58819c3c91076b7c1f"), "nm" : "00000C31" }
{ "_id" : ObjectId("563c8d5d819c3c91076b86c1"), "nm" : "000013E0" }
{ "_id" : ObjectId("563c8d5d819c3c91076b86c5"), "nm" : "0000110B" }

整个Go程序如下所示:

package main

import (
    "log"
    "gopkg.in/mgo.v2"
)

type StationConfig struct {
        id     string   `bson:"_id,omitempty"`
        name   string   `bson:"nm"`
}

func main() {
        session, err := mgo.Dial("192.168.1.41")
        if err != nil {
                panic(err)
        }
        defer session.Close()

        c := session.DB("metos").C("stations")

        var stationConfigs []StationConfig            
        err = c.Find(nil).Limit(100).All(&stationConfigs)

        if err != nil {
                log.Fatal(err)
        }

        log.Printf("Found document: %+v\n", stationConfigs)
}

但是当我运行程序时,_id和nm的值没有分配给相应的结构体属性,我得到以下输出:

Found document: [{id: name:} {id: name:} {id: name:} {id: name:} 
    {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} 
    {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} 
    {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} 
    {id: name:} {id: name:} {id: name:} ... 以此类推 ... ]

我漏掉了什么?

英文:

This might be a very silly problem, but after two hours of searching the net I am posting the question here.

I tried learning Go and have a very simple "Hello World" application, which uses Mongo for the data source. I can connect normally, I can fetch the data, and the count of objects turns out ok.

The problem is, the object I map data to has empty properties, although there is data in Mongo.

I have a very simple collection in mongo, called stations with ~12k records like these:

{ "_id" : ObjectId("563c8d56819c3c91076b7c13"), "nm" : "00000BE8" }
{ "_id" : ObjectId("563c8d57819c3c91076b7c1a"), "nm" : "00000C01" }
{ "_id" : ObjectId("563c8d58819c3c91076b7c1d"), "nm" : "00000C02" }
{ "_id" : ObjectId("563c8d58819c3c91076b7c1f"), "nm" : "00000C31" }
{ "_id" : ObjectId("563c8d5d819c3c91076b86c1"), "nm" : "000013E0" }
{ "_id" : ObjectId("563c8d5d819c3c91076b86c5"), "nm" : "0000110B" }

The entire Go program looks like this:

package main

import (
    "log"
    "gopkg.in/mgo.v2"
)

type StationConfig struct {
        id     string   `bson:"_id,omitempty"`
        name   string   `bson:"nm"`
}

func main() {
        session, err := mgo.Dial("192.168.1.41")
        if err != nil {
                panic(err)
        }
        defer session.Close()

        c := session.DB("metos").C("stations")

        var stationConfigs []StationConfig            
        err = c.Find(nil).Limit(100).All(&stationConfigs)

        if err != nil {
                log.Fatal(err)
        }

        log.Printf("Found document: %+v\n", stationConfigs)
}

But when I run the program, the values of _id and nm are not assigned to the respective struct properties and I get the following output:

Found document: [{id: name:} {id: name:} {id: name:} {id: name:} 
    {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} 
    {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} 
    {id: name:} {id: name:} {id: name:} {id: name:} {id: name:} 
    {id: name:} {id: name:} {id: name:} ... and so on ... ]

What am I missing?

答案1

得分: 1

我不熟悉MongoDB Go API,但我认为你的结构字段应该是公开的,以便MongoDB API能够填充它们。

尝试将你的字段设置为公开,看看是否有效:

type StationConfig struct {
    ID   string `bson:"_id,omitempty"`
    Name string `bson:"nm"`
}
英文:

I'm not familiar with the MongoDB Go API but I think your struct fields should be public in order for the MongoDB API to be able to populate them.

Try making your fields public and see if it works:

type StationConfig struct {
        ID     string   `bson:"_id,omitempty"`
        Name   string   `bson:"nm"`
}

huangapple
  • 本文由 发表于 2015年11月18日 01:27:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/33763287.html
匿名

发表评论

匿名网友

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

确定