doufangzhang4454 2015-11-11 02:45
浏览 32

我可以将json标签用作mgo中的json标签吗?

I am using thrift in my project, thrift will generate code as follow:

type CvJdRelationInfo struct {
    JdId            string `thrift:"jdId,1" json:"jdId"`
    CvId            string `thrift:"cvId,2" json:"cvId"`
    Status          int16  `thrift:"status,3" json:"status"`
    AcceptTimestamp int64  `thrift:"acceptTimestamp,4" json:"acceptTimestamp"`
}

as you see thrift already generate json tags(but no bson tags), when I use mgo save record, mgo will auto convert:

JdId -> jdid
CvId -> cvid
Status -> status
AcceptTimeStamp -> accepttimestamp

what I needed is:

type CvJdRelationInfo struct {
    JdId            string `thrift:"jdId,1" json:"jdId" bson:"jdId"`
    CvId            string `thrift:"cvId,2" json:"cvId" bson:"cvId"`
    Status          int16  `thrift:"status,3" json:"status" bson:"status"`
    AcceptTimestamp int64  `thrift:"acceptTimestamp,4" json:"acceptTimestamp" bson:"acceptTimestamp"`
}

as your can see, bson tags is the same as json tags. Can I use json tags as bson tags?

  • 写回答

2条回答 默认 最新

  • douzhanbai9526 2015-11-11 03:25
    关注

    MongoDB actually stores the data as binary JSON (bson), which is distinct from JSON. It's slightly confusing because if you access the database using the mongo shell, you get back raw JSON, but it's actually a conversion, it's not the storage format. So in storing the data to the database, the "mgo" driver serializes as bson.

    This serializing ignores the json export keys, and chooses the appropriate name by defaulting to the lowercase version of your struct field. (See the bson.Marshal go doc.) If you specify a bson export key, it will then ignore the struct field name and go with whatever you have specified as the bson export key.

    For example,

    type User struct {
        Name string
        UserAge int `bson:"age"`
        Phone string `json:"phoneNumber"`
    }
    

    will result in the following structure in MongoDB:

    {
        "name": "",
        "age": 0,
        "phone": ""
    }
    

    So it seems like your struct fields should handle most things for you.

    The one 'gotcha' that you might not see until it bites you is, if you don't specify the bson export keys, you don't have the ability to do bson:",omitempty" for leaving out blank fields, or bson:",inline" for marshaling embedded (or nested) structs.

    For instance, this is how you would handle embedded structs:

    type Employee struct {
        User `bson:",inline"`
        JobTitle string
        EmployeeId string
        Salary int
    }
    

    These sorts of things are specified in that link I provided on bson.Marshal. Hope that helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上