mongodb - 使用 Golang 在 MongoDb 中插入嵌套结构

标签 mongodb go mgo

我对使用 mgo 在 MongoDB 中存储有疑问。

我的数据库有这样的结构:

{
  "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 应用程序中,结构是:

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"'
  }

但我无法创建变量来将所有内容放在一起:

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{},
    }

我的主要目标是当我在里面插入'personita'时有一个默认值:

"abonnements": {
    "newsletter": false
  }

最佳答案

似乎只是一个错字

Abonnements : []Abonnement{}

关于mongodb - 使用 Golang 在 MongoDb 中插入嵌套结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39321653/

相关文章:

javascript - Mongoose getter正在获取未定义的参数

node.js - mongoose Document#update 的所有可能回调参数是什么?

xml - 使用 Go 并行读取多个 URL

css - 如何在 Go、Google App Engine 中使用外部 CSS 和(静态)图像

go - GO 中序列中的多个测试用例

mongodb - 如何删除 Mongodb/Golang 中的数组项?

node.js - 如何使用 MongoDB/NodeJS 进行大规模随机更新

mongodb - 是否可以在 MongoDB 的单个事务中执行多个数据库操作?

go - 使用golang的mgo库,如何获取列表等嵌套对象