dqhgjay5753 2018-03-21 06:17
浏览 258

从mgo(MongoDB库)到Golang结构的嵌套结构转换?

I have a struct like this:

type ArticleDocument struct {
    ArticleID    string `json:"article_id" bson:"article_id"`
    ArticleTitle string `json:"article_title" bson:"article_title"`
    Author       string `json:"author" bson:"author"`
    Board        string `json:"board" bson:"board"`
    Content      string `json:"content" bson:"content"`
    Date         string `json:"date" bson:"date"`
    IP           string `json:"ip" bson:"ip"`
    MessageConut struct {
        All     int `json:"all" bson:"all"`
        Boo     int `json:"boo" bson:"boo"`
        Count   int `json:"count" bson:"count"`
        Neutral int `json:"neutral" bson:"neutral"`
        Push    int `json:"push" bson:"push"`
    } `json:"message_count,inline" bson:"message_count,inline"`
    Messages []interface{} `json:"messages" bson:"messages"`
    URL      string        `json:"url" bson:"url"`
}

In MongoDB, my document looks like:

{
    "_id" : ObjectId("5ab1da8133691b034b2be31d"),
    "article_id" : "M.1521548086.A.DCA",
    "article_title" : "some title",
    "author" : "somebody",
    "board" : "some board",
    "content" : "some content",
    "date" : "Tue Mar 20 20:14:42 2018",
    "ip" : "1.1.1.1",
    "message_conut" : {
        "all" : 15,
        "boo" : 0,
        "count" : 14,
        "neutral" : 1,
        "push" : 14
    },
    "messages" : [],
    "url" : "https://xxx.xxx.xxx"
}

In my Golang code, i am trying to use mgo to query this document and print it out, however, all document fields are converted to golang struct correctly except the nested document "MessageConut", all the values in "All", "Boo", "Count", ...etc are zero:

message_count":{"all":0,"boo":0,"count":0,"neutral":0,"push":0}"

Could you please guide how do I solve this issue?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向