Go 将地图转换为bson

Go 将地图转换为bson,go,mgo,Go,Mgo,bson文件应为: type Player struct { id bson.ObjectId test map[int]int } func (this *Player) Id() bson.ObjectId { return this.id } func (this *Player) DbObj() bson.D { testBson := bson.D{} for k,v := range this.test { testBso

bson文件应为:

type Player struct {
    id bson.ObjectId
    test map[int]int
}

func (this *Player) Id() bson.ObjectId {
    return this.id
}

func (this *Player) DbObj() bson.D {

    testBson := bson.D{}
    for k,v := range this.test {
        testBson = append(testBson, bson.M{"id":k, "v":v}) // compile error
    }
    return bson.D{
        {"_id",this.id},
        {"test", testBson},
    }
}

但是我不知道如何将map对象转换为[{'id':1,'v':1},…,{'id':2,'v':2}],bson.M不是D包含的DocElem类型

使用Go切片表示bson数组:

{'_id':ObjectId(xx),'test':[{'id':1,'v':1},..., {'id':2,'v':2}]}
func (this *Player) DbObj() bson.D {
    var testBson []bson.M
    for k,v := range this.test {
        testBson = append(testBson, bson.M{"id":k, "v":v}) 
    }
    return bson.D{
        {"_id",this.id},
        {"test", testBson},
    }
}