Ejemplo n.º 1
0
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()
}
Ejemplo n.º 2
0
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
}
Ejemplo n.º 3
0
//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
}