0

I have a question regarding storage in MongoDB using mgo.

My DB has this structure :

{
  "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é"
}

In my Go application the structures are :

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

But I can't manage to create the variable to put everything together :

personita := Persone{
      Compte : "Standard",
      Datei : time.Date(2015, time.February, 12, 04, 11, 0, 0, time.UTC),
      Email : "[email protected]",
      MDP : "test_mdp",
      Type : "T&D",
      User : "test_user",
      Validation : "validé",
      Arrival  : []string{},
      Clicks : []string{},
      Recherches : []string{},
      Abonnements : []Abonnement{},
    }

My main goal would be that there's a default value when I insert 'personita' with this inside :

"abonnements": {
    "newsletter": false
  }
2
  • 1
    You're missing a {} at the end of []Abonnement Commented Sep 4, 2016 at 21:31
  • How can I add the default value for abonnements? "abonnements": { "newsletter": false } @Sridhar Commented Sep 4, 2016 at 21:48

3 Answers 3

1

Seems just a typo

Abonnements : []Abonnement{}
Sign up to request clarification or add additional context in comments.

6 Comments

How can I add the default value for abonnements? @uvelichitel "abonnements": { "newsletter": false }
Abonnements : []Abonnement{{false}}
The problem with this solution is that it creates : "abonnements": [ { "newsletter": false } ] and I would like : {"abonnements": { "newsletter": false } }
You define Abonnements as slice of Abonnement in Person struct. So it initialized as slice and than marshalled in bson array.
Abonnements : Abonnement{false}
|
1

Maybe something like this. First define a function that returns a pointer to the structure:

func NewAbonnement()(ab *Abonnement){
    return &Abonnement{Newsletter: false}
}

Then call the function as Abonnement slice literal:

 personita := Persone{
      Compte : "Standard",
      Datei : time.Date(2015, time.February, 12, 04, 11, 0, 0, time.UTC),
      Email : "[email protected]",
      MDP : "test_mdp",
      Type : "T&D",
      User : "test_user",
      Validation : "validé",
      Arrival  : []string{},
      Clicks : []string{},
      Recherches : []string{},
      Abonnements : []Abonnement{*NewAbonnement()},
    }

Playground

2 Comments

The problem with this solution is that it creates : "abonnements": [ { "newsletter": false } ] and I would like : {"abonnements": { "newsletter": false } }
In the edit I made a map, then marshaled the map to json and assigned the string to Abonnments as string.
0

The solution for having Nested structures is to do:

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

and then :

personita := Personne{
    Compte : "Standard",
    Datei : time.Date(2015, time.February, 12, 04, 11, 0, 0, time.UTC),
    Email : "[email protected]",
    MDP : "test_mdp",
    Type : "T&D",
    User : "test_user",
    Validation : "validé",
    Abonnements : Abonnement{false,"",""},
   }

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

This way you have the nested JSON added to MongoDB with default values.

Furthermore, in this particular case, StripeID or StripeSub are optional, hence if the value is empty they won't appear on your database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.