Пример #1
0
func Example() {

	// Connect to server.

	conn, err := mongo.Dial("localhost")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	c := mongo.Collection{conn, "example-db.example-collection", mongo.DefaultLastErrorCmd}

	// Insert a document.

	id := mongo.NewObjectId()

	err = c.Insert(&ExampleDoc{Id: id, Title: "Hello", Body: "Mongo is fun."})
	if err != nil {
		log.Fatal(err)
	}

	// Find the document.

	var doc ExampleDoc
	err = c.Find(map[string]interface{}{"_id": id}).One(&doc)
	if err != nil {
		log.Fatal(err)
	}

	log.Print(doc.Title, " ", doc.Body)
}
Пример #2
0
// Dynacmiaclly create headers for a csv
// file by getting setting the field names
// into a slice
// accepts a collection
// returns collection, err
func CreateHeadersDyn(c mongo.Collection) []string {
	var m mongo.M
	err := c.Find(nil).One(&m)
	if err != nil && err != mongo.Done {
		log.Fatal(err)
	}

	var header []string
	for k, _ := range m {
		header = append(header, fmt.Sprint(k))
	}
	return header
}