代码之家  ›  专栏  ›  技术社区  ›  Sredny M Casanova

mgo.v2没有在struct中解组嵌套映射,而是设置bson.M

  •  0
  • Sredny M Casanova  · 技术社区  · 6 年前

    目前我在一个Go项目中工作,使用mongo和 gopkg.in/mgo.v2 从数据库检索值时遇到问题,我的结构如下:

    type person struct {
        Name string
        Age  int
        MyMap map[string]interface{}
    } 
    

    稍后,我将调用一个函数来获取结果(它是接口的一部分):

    func(m SomeInterface) Get(filter bson.M, target interface{}){
         collection := m.Db.C("myCollection")
         err := profilesCollection.Find(filter).One(val)
         if err != nil {
            panic("error reading from mongo")
        }
    }
    

    我把这个函数称为:

    p := person{}
    myinterface.Get(somefilter, &p)
    

    完成此操作后,p.MyMap的类型为 bson.M 而不是 map[string]interface{} …为什么?我怎么能得到地图而不是bson.M?谢谢

    0 回复  |  直到 6 年前
    推荐文章