Exemplo n.º 1
0
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()
}
Exemplo n.º 2
0
func (c *Context) popMongoCollection() (*mongo.Collection, os.Error) {
	collection := <-c.mongoPool

	if collection == nil {
		conn, err := mongo.Connect(c.mongoAddr)
		if err != nil {
			return nil, err
		}
		collection = conn.GetDB(c.mongoDb).GetCollection("logs")
	}

	return collection, nil
}
Exemplo n.º 3
0
func (md *MongoDB) Connect() {
	var err os.Error
	md.conn, err = mongo.Connect("127.0.0.1")
	if err != nil {
		fmt.Println("Couldn't connect to mongo db @ localhost")
		os.Exit(-1)
		return
	}

	md.db = md.conn.GetDB("blog")
	md.posts = md.db.GetCollection("posts")
	md.comments = md.db.GetCollection("comments")

}
Exemplo n.º 4
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()
}
Exemplo n.º 5
0
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()
}
Exemplo n.º 6
0
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()
}