Sorry, but I have very noob questions.
Suppose there is a struct
type Post struct {
ID bson.ObjectID
CommentsIds []bson.ObjectID `bson:"comments" json:"-"`
Comments []*Comment `bson:"-" json:"comments"`
}
1.
When I fetch Post object from DB I also fetch Comments field by hand.
As I understand MGO doesn't have auto population so I have to put in this struct two fields: array of ids and array on objects (for later convenience page.Comments).
Is it a good way to make this things? Which convention do you use? I really can't stand these ugly tags :(
2.
Okay, then I want to add one or two comments and then save post. I saved comments as a separated documents and then append their ids into CommentIds.
.Update(post, {_id: ...}) saves entire document. But how to force MGO to update post object with just $push method? Or I have to make this operation manually?
3.
About logging.
What I do wrong?
Here is my code:
func initDBSession() {
session, err := mgo.Dial("mongodb://localhost:27017/mydb")
if err != nil {
panic(fmt.Sprintf("Failed connect to Mongo (%v)", err))
}
//mgo.SetDebug(true) // this causes A LOT of output, so it doesn't suit for me
logout := log.New(os.Stdout, "MGO: ", log.Lshortfile)
mgo.SetLogger(logout)
dbSession = session // global variable
}
I spoiled by NodeJS Mongoose library where all such things are automated :(
Thanks in avance!