我想將如下所示的數(shù)據(jù)結(jié)構(gòu)插入到 MongoDB{ "rolecode": "DHBK1_ROLE_04", "functioncodelist": [ "DHBK1_FUNC_1", "DHBK1_FUNC_2", ..... "DHBK1_FUNC_n"] "productid": "ABC_Platform", "comid": "DHBK1"}這是我的代碼:package mainimport ( "context" "gopkg.in/mgo.v2")func main() { insertObj()}type CompanyRoleFunction struct { Rolecode string `json:"rolecode"` Productid string `json:"productid"` Functioncodelist []string Comid string `json:"comid"`}func insertObj() { session, err := mgo.Dial("mongodb://casuser:Mellon@222.255.102.145:27017/users") if err != nil { panic(err) } c := session.DB("users").C("company_role_function") var companyRoleFunction CompanyRoleFunction companyRoleFunction.Rolecode = "DHBK1_ROLE_05" companyRoleFunction.Productid = "XYZ_Platform" companyRoleFunction.Comid = "DHBK1" err = c.Insert(companyRoleFunction) if err != nil { fmt.Println(err) }}我的代碼已經(jīng)運(yùn)行,但它只插入這樣的結(jié)構(gòu)(當(dāng)然,因?yàn)槲也恢廊绾翁幚頂?shù)組 Functioncodelist )[![在此處輸入圖像描述][1]][1]這是我的數(shù)據(jù),我希望插入{"_id":{"$oid":"5ed0c2e2402a000037004c84"},"rolecode":"DHBK1_ROLE_07","functioncodelist":["DHBK1_FUNC_1","DHBK1_FUNC_2"],"productid":"ABC_Platform","comid":"DHBK1"}
1 回答

郎朗坤
TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊
它沒有顯示在數(shù)據(jù)庫中,因?yàn)槟迦氲奈臋n不包含任何值。在你的 go 代碼中設(shè)置一個(gè)值,它應(yīng)該在數(shù)據(jù)庫中顯示得很好,除非我誤解了你的問題。
...
type Functioncode string
...
companyRoleFunction.Functioncodelist = make(map[string]Functioncode)
...
companyRoleFunction.Functioncodelist["foo"] = "bar"
...
有關(guān) Go 地圖的快速介紹,請(qǐng)參見https://tour.golang.org/moretypes/19。
- 1 回答
- 0 關(guān)注
- 190 瀏覽
添加回答
舉報(bào)
0/150
提交
取消