mgo bson can't serialize into struct

79 views
Skip to first unread message

lac

unread,
Mar 20, 2016, 11:28:51 PM3/20/16
to mgo-users
Hi, it 's my struct:
type findingSchool struct {
    Id                bson.ObjectId     `_id`
    StudentNum        map[string]string `bson:"年级人数构成"`
    E_des             string            `bson:"e_des"`
    Baseinfo          findschBaseinfo   `bson:"baseinfo"`
    QualityofTeaching qualityofteaching `bson:"教学质量"`
    Apply             findingApply      `bson:"apply"`
    Tuition           findTuition       `bson:"tuition|2013"`
    //    Targetranking     targetranking     `targetranking`
    Dorm            findschoolDorm    `bson:"学校住宿"`
    University      map[string]string `bson:"全国性综合大学"`
}

Because because the real key in my mongo collection is not stable, like tuition may be 'tuition|2013' or 'tuition|2012', so i can't get the result.
how can i do?

Klaus Post

unread,
Mar 21, 2016, 3:51:37 PM3/21/16
to mgo-users
Hi!

Just looking quickly at it, the "Id" struct tag is wrong. It should be `bson:"_id"`.

If you have multiple representations of the same value, you could either have multiple struct members with a pointer, or extract to a bson.M and use code to extract that value by iterating the map.

/Klaus

Gustavo Niemeyer

unread,
Mar 21, 2016, 4:02:12 PM3/21/16
to mgo-...@googlegroups.com
Although discouraged, it actually still works unadorned like that, for historical reasons.

I think the problem being reported is the fact we cannot change the tag at runtime.

This may only be addressed by using a map instead of a fixed field.



--
You received this message because you are subscribed to the Google Groups "mgo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mgo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages