I'm attempting to create nested objects in mongoDB with no luck the format I am trying to achieve is as follows
"Courses":{
"Date":{
"CourseName" :{
"hole 1"{
}
"hole 2"{
}
...so on until 18
}//coursename
}//date
}//courses
I've tried and succeeded with getting the date object within course by doing the following:
u := req.FormValue("username")
co := req.FormValue("course")
d := req.FormValue("date")
ng := nGame{Username: u, Course: co, Dates: d}
cn := courseName{CName: co}
query := bson.M{"username": u}
update := bson.M{"$push": bson.M{"Course": bson.M{ng.Dates: cn}}}
err = c.Update(query, update)
The date object has the course name inside it what i'm trying to do is make course name another object which then I can insert the hole object.
The Structs i'm using are as follows:
type (
nGame struct {
Username string
Course string
Location string
Dates string
}
)
type (
courseName struct {
CName string
}
)
Your described structure, as I understand it, can be represented in Go as follows:
type Hole struct {
// Whatever you want here
}
type Course struct {
Hole1 Hole `json:"hole 1"`
Hole2 Hole `json:"hole 2"`
// ...
Hole18 Hole `json:"hole 18"`
}
type Courses struct {
// Date CourseName
map[string]map[string]Course
}
I would suggest, however, using an 18-element array for your holes, but that's up to you:
type Course struct {
Holes [18]Hole
}
Then you can instantiate one of these trees as (using a [18]Hole
array; adjust accordingly if you use a different implementation):
courses := Courses{
map[string]map[string]Course{
"2017-01-01": map[string]Course{
"Bob's Course": Course{
[18]Hole{
Hole{
// Hole 1
},
Hole{
// Hole 2
},
// ..
},
},
},
},
}