I have a structure like this:
type data_to_store struct {
Data some_custom_structure `json:"Data" bson:"Data"`
MoreData another_custom_structure `json:"more_data" bson:"More_Data"`
}
creating an object that uses this struct:
Data := data_to_store {
Data: some_custom_struct_object,
MoreData: another_custom_struct_object,
}
And I'm trying to insert it into db like that:
session, _ := mgo.Dial("localhost")
defer session.Close()
session.SetMode(mgo.Monotonic, true)
collection := session.DB("test_database").C("test_collection")
collection.Insert(&Data)
And it does store, but it turns out to be inserted like this:
{ "lol": "rofl",
"lmao": "kek",
}
} { "blah": "blahblah",
"ololo": 2,
}
Is there a way to make it be stored like the following?
"data": { "lol": "rofl",
"lmao": "kek",
}
}, "more_data" { "blah": "blahblah",
"ololo": 2,
}
Marshalling data doesn't help in this endeavour, or I'm doing it wrong:
data, _ := bson.Marshal(&replay)
collection.Insert(&data)
If this below is the Json structure that you want to store in Mongo:
{
"data": [{
"lol": "rofl"
},
{
"lmao": "kek"
}
],
"more_data": [{
"blah": "blahblah"
},
{
"ololo": 2
}
]}
You need these type of Go struct:
type Data struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
type DataToStore struct {
Data []Data `json:"data"`
MoreData []Data `json:"more_data"`
}
You can use this online tool to convert json into go struct.