//warning: it will marhsall the comments list - so we need to change this //if we enable updating/editing posts func (md *MongoDB) StorePost(post *BlogPost) (id int64, err os.Error) { md.postmu.Lock() defer md.postmu.Unlock() //create new post if post.Id == 0 { qry, _ := mongo.Marshal(map[string]string{}) count, _ := md.posts.Count(qry) count++ id = count post.Id = count doc, _ := mongo.Marshal(*post) err = md.posts.Insert(doc) return } else { //update post type q map[string]interface{} m := q{"id": post.Id} var query mongo.BSON query, err = mongo.Marshal(m) if err != nil { return } doc, _ := mongo.Marshal(*post) err = md.posts.Update(query, doc) } return }
func (md *MongoDB) StoreComment(comment *PostComment) (id int64, err os.Error) { md.commentmu.Lock() defer md.commentmu.Unlock() //check if post with that id exists _, err = md.GetPost(comment.PostId) if err != nil { //err = os.NewError("Post doesn't exist :]") return } content := comment.Content //content = strings.Replace(content, "<", "(", -1) // content = strings.Replace(content, ">", ")", -1) //author := html.EscapeString(comment.Author) author := comment.Author // author = strings.Replace(author, "<", "(", -1) //author = strings.Replace(author, ">", ")", -1) comment.Author = author //html.EscapeString(comment.Author) comment.Content = content //html.EscapeString(comment.Content) qry, _ := mongo.Marshal(map[string]string{}) count, _ := md.comments.Count(qry) count++ id = count comment.Id = count doc, _ := mongo.Marshal(*comment) fmt.Println(doc) md.comments.Insert(doc) return }
func main() { conn, _ := mongo.Connect("127.0.0.1") coll := conn.GetDB("example").GetCollection("typed_structs") firstDoc := &ExampleDoc{ Title: "Page Title", Body: "Some example text", Comments: []Comment{ Comment{ Author: "foobar", Message: "Thanks For the post", }, Comment{ Author: "hokapok", Web: "go.hokapok.com", Message: "No Problem", }, Comment{ Author: "golang", Web: "golang.org", Message: "Thanks", }, }, } bsonDocIn, _ := mongo.Marshal(firstDoc) coll.Insert(bsonDocIn) fmt.Println("Inserted Document") coll.Drop() }
func main() { conn, _ := mongo.Connect("127.0.0.1") collection := conn.GetDB("test").GetCollection("test_collection") doc, _ := mongo.Marshal(map[string]string{ "_id": "doc1", "title": "A Mongo document", "content": "Testing, 1. 2. 3.", }) collection.Insert(doc) query, _ := mongo.Marshal(map[string]string{"_id": "doc1"}) got, _ := collection.FindOne(query) mongo.Equal(doc, got) // true! collection.Drop() }
func (c *Context) Update(stat *Statmsg) { _, err := c.redisClient.Incr("hit:" + stat.Key) if err != nil { fmt.Printf("Error from redis: %s\n", err.String()) } collection, err := c.popMongoCollection() if err != nil { fmt.Printf("Error from mongo: %s\n", err.String()) } doc, _ := mongo.Marshal(stat) collection.Insert(doc) c.pushMongoCollection(collection) }
func main() { conn, _ := mongo.Connect("127.0.0.1") coll := conn.GetDB("example").GetCollection("typed_structs") qFindDoc, _ := mongo.Marshal(&searchDoc{title: "Page Title"}) bsonDocOut, _ := coll.FindOne(qFindDoc) var foundDoc ExampleDoc mongo.Unmarshal(bsonDocOut.Bytes(), &foundDoc) fmt.Println(foundDoc.Title + "::" + foundDoc.Body) fmt.Printf("Comment %v\n", foundDoc) coll.Drop() }
func main() { conn, _ := mongo.Connect("127.0.0.1") coll := conn.GetDB("example").GetCollection("map_string") firstDoc := map[string]string{ "_id": "1", "title": "Page Title", "body": "Some example Text", } bsonDocIn, _ := mongo.Marshal(firstDoc) coll.Insert(bsonDocIn) fmt.Println("Inserted Document") coll.Drop() }
func (md *MongoDB) getPostsForQuery(qryobj interface{}, skip, limit int32) (posts []BlogPost, err os.Error) { md.postmu.Lock() defer md.postmu.Unlock() var query mongo.BSON query, err = mongo.Marshal(qryobj) if err != nil { return } // count, _ := md.posts.Count(query) // if count == 0 { // err = os.NewError("COUNT 0 Post Not Found") // return // } var docs *mongo.Cursor //docs, err = md.posts.FindAll(query) docs, err = md.posts.Query(query, skip, limit) if err != nil { return } var doc mongo.BSON for docs.HasMore() { doc, err = docs.GetNext() if err != nil { return } var post BlogPost err = mongo.Unmarshal(doc.Bytes(), &post) if err != nil { return } posts = append(posts, post) } // if len(posts) == 0 { // err = os.NewError("no posts found") // } return }
//get comments belonging to a post func (md *MongoDB) GetComments(post_id int64) (comments []PostComment, err os.Error) { md.commentmu.Lock() defer md.commentmu.Unlock() //m := map[string]int64{"postid": post_id} type q map[string]interface{} m := q{ "$query": q{"postid": post_id}, "$orderby": q{"timestamp": 1}, } var query mongo.BSON query, err = mongo.Marshal(m) if err != nil { return } var docs *mongo.Cursor docs, err = md.comments.FindAll(query) if err != nil { return } var doc mongo.BSON for docs.HasMore() { doc, err = docs.GetNext() if err != nil { return } var comment PostComment err = mongo.Unmarshal(doc.Bytes(), &comment) if err != nil { return } comments = append(comments, comment) } return }