Here Is my json file and i want to insert the data using golang and mgo in this json format
[{
"_id" : ObjectId("57307906f051147d5317984e"),
"dateAdded" : " 20015-11-10 23:00:00 +0000 UTC"
"firstName" : "chetan",
"lastName" : "kumar",
"age" : 23,
"user" : [
{
"userid" : ObjectId("57307906f051147d5317984a"),
"firstName" : "chetan",
"lastName" : "kumar",
"age" : 23
},
{
"userid" : ObjectId("57307906f051147d5317984b"),
"firstName" : "nepolean",
"lastName" : "dang",
"age" : 26
},
{
"userid" : ObjectId("57307906f051147d5317984c"),
"firstName" : "Raj",
"lastname" : "kumar",
"age" : 26
}
],
"sales" : [
{
"salesid" : ObjectId("57307906f051147d5317984d"),
"firstName" : "ashu",
"lastName" : "jha",
"age" : 27
}
]
}]
Now ,here is my go file which i was trying to insert data through golang and mgo
package main
import(
"fmt"
"time"
"net/http"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type userinfo struct{
ID bson.ObjectId `json:"_id" bson:"_id"`
USER []User `json:"user" bson:"user"`
SALES []Sales `json:"sales" bson:"sales"`
DATEADDED time.Time `json:"dateAdded" bson:"dateAdded"`
NAME string `json:"name" bson:"name"`
}
type User struct{
USERID bson.ObjectId `json:"userid" bson:"userid"`
FIRSTNAME string `json:"firstName" bson:"firstName"`
LASTNAME string `json:"lastName" bson:"lastName"`
AGE int `json:"age" bson:"age"`
}
type Sales struct{
SALESID bson.ObjectId `json:"salesid" bson:"salesid"`
FIRSTNAME string `json:"firstName" bson:"firstName"`
LASTNAME string `json:"lastName" bson:"lastName"`
AGE int `json:"age" bson:"age"`
}
func post(w http.ResponseWriter,r *http.Request){
session,err := mgo.Dial("127.0.0.1")
if err != nil{
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic,true)
c:= session.DB("userdb").C("user")
fmt.Fprintln(w,"conn")
err = c.Insert(&userinfo{ID:new ObjectId(),NAME:"Admin",USER:{USERID:new ObjectId(), FIRSTNAME: "sam",LASTNAME : "billing",AGE : 25},SALES:{SALESID:new ObjectId(),FIRSTNAME : "joe",LASTNAME : "root",AGE : 23},DATEADDED:time.Now()})
if err != nil {
panic(err)
}
}
func main(){
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/post/",post)
http.ListenAndServe(":8080",router)
}
but it's not work at all please help me out