第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Golang 在 MongoDb 中插入嵌套結(jié)構(gòu)

使用 Golang 在 MongoDb 中插入嵌套結(jié)構(gòu)

Go
ibeautiful 2022-03-07 22:38:39
我有一個關(guān)于使用 mgo 在 MongoDB 中存儲的問題。我的數(shù)據(jù)庫有這樣的結(jié)構(gòu):{  "Arrival": [    "04-09-2016"  ],  "Clicks": [    "56ffd41d9c8c9adf088b4576",    "4f1dc63a7c2d3817640000a1"  ],  "Recherches": [    "érysipèle"  ],  "abonnements": {    "newsletter": false  },  "compte": "Standard",  "datei": ISODate("2016-09-04T14:55:39.179Z"),  "email": "_°°°°_",  "mdp": "27d8d166ca1f1715541b7df6453401b95a1d59c2ca0f60ce71037d33926c4d6f09a63a909a8d5cb5230f92584973a4dd2b8bcf155d5785ef7e2afdd113334eee",  "type": "T&D",  "user": "_°°°°_",  "validation": "validé"}在我的 Go 應(yīng)用程序中,結(jié)構(gòu)是:type Abonnement struct {    Newsletter bool bson:"newsletter"'  }type Persone struct {    Compte string 'bson:"compte"'    Datei time.Time 'bson:"datei"'    Email string  'bson:"email"'    MDP string 'bson:"mdp"'    Type string 'bson:"T&D"'    User string 'bson:"user"'    Validation string 'bson:"validation"'    Arrival []string 'bson:"Arrival"'    Clicks []string 'bson:"Clicks"'    Recherches []string 'bson:"Recherches"'    Abonnements []Abonnement 'bson:"abonnements"'  }但是我無法創(chuàng)建變量來將所有內(nèi)容放在一起:personita := Persone{      Compte : "Standard",      Datei : time.Date(2015, time.February, 12, 04, 11, 0, 0, time.UTC),      Email : "test@test.com",      MDP : "test_mdp",      Type : "T&D",      User : "test_user",      Validation : "validé",      Arrival  : []string{},      Clicks : []string{},      Recherches : []string{},      Abonnements : []Abonnement{},    }我的主要目標是當(dāng)我在里面插入“personita”時有一個默認值:"abonnements": {    "newsletter": false  }
查看完整描述

3 回答

?
白衣染霜花

TA貢獻1796條經(jīng)驗 獲得超10個贊

似乎只是一個錯字

Abonnements : []Abonnement{}


查看完整回答
反對 回復(fù) 2022-03-07
?
倚天杖

TA貢獻1828條經(jīng)驗 獲得超3個贊

也許是這樣的。首先定義一個返回結(jié)構(gòu)指針的函數(shù):


func NewAbonnement()(ab *Abonnement){

    return &Abonnement{Newsletter: false}

}

然后將該函數(shù)稱為 Abonnement 切片文字:


 personita := Persone{

      Compte : "Standard",

      Datei : time.Date(2015, time.February, 12, 04, 11, 0, 0, time.UTC),

      Email : "test@test.com",

      MDP : "test_mdp",

      Type : "T&D",

      User : "test_user",

      Validation : "validé",

      Arrival  : []string{},

      Clicks : []string{},

      Recherches : []string{},

      Abonnements : []Abonnement{*NewAbonnement()},

    }


查看完整回答
反對 回復(fù) 2022-03-07
?
慕后森

TA貢獻1802條經(jīng)驗 獲得超5個贊

擁有嵌套結(jié)構(gòu)的解決方案是:


type Abonnement struct {

  Newsletter bool `bson:"newsletter"`

  StripeID string `bson:"stripe_id,omitempty"`

  StripeSub string `bson:"stripe_sub,omitempty"`

}


type Personne struct {

  Compte string `bson:"compte"`

  Datei time.Time `bson:"datei"`

  Email string  `bson:"email"`

  MDP string `bson:"mdp"`

  Type string `bson:"T&D"`

  User string `bson:"user"`

  Validation string `bson:"validation"`

  Arrival []string `bson:"Arrival"`

  Clicks []string `bson:"Clicks"`

  Recherches []string `bson:"Recherches"`

  Abonnements Abonnement `bson:"abonnements"`

}

然后 :


personita := Personne{

    Compte : "Standard",

    Datei : time.Date(2015, time.February, 12, 04, 11, 0, 0, time.UTC),

    Email : "test@test.com",

    MDP : "test_mdp",

    Type : "T&D",

    User : "test_user",

    Validation : "validé",

    Abonnements : Abonnement{false,"",""},

   }


if err := coll.Insert(personita); err != nil {panic(err)}

這樣,您就可以使用默認值將嵌套的 JSON 添加到 MongoDB。


此外,在這種特殊情況下,StripeID 或 StripeSub 是可選的,因此如果值為空,它們將不會出現(xiàn)在您的數(shù)據(jù)庫中。


查看完整回答
反對 回復(fù) 2022-03-07
  • 3 回答
  • 0 關(guān)注
  • 310 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號