Using mongoose with NodeJs for document population to simulate joins is very common. I'm trying to understand how to achieve something similar with go and mgo.
type User struct {
Id bson.ObjectId `json:"_id" bson:"_id"`
UserName string
}
type MessageBoard {
Id bson.ObjectId `json:"_id" bson:"_id"`
}
type Tag struct {
Id bson.ObjectId `json:"_id" bson:"_id"`
text string
}
type Post struct {
Id bson.ObjectId `json:"_id" bson:"_id"`
Text string
MessageBoard bson.ObjectId
User bson.ObjectId
Tags []bson.ObjectId
}
I would like to store only User and Tag ids in Post. A user can have many posts and if the user edits UserName all posts should reflect this.
var posts []Post
err := PostCollection.Find(bson.M{"MessageBoard":mBoardId}).All(&posts)
if err != nil {
return nil, err
}
This is a simple query to get posts, but how do I efficiently get posts with User and Tags models populated?
There is Sleep (an Mgo extension) which appears to do what I am looking for, but I am interested in understanding the performance implications and design options better and if there is a solution using Mgo alone. https://github.com/mansoor-s/Sleep