Beispiel #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)
}
Beispiel #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
}
Beispiel #3
0
func evict(m map[string]interface{}, collection *mongo.Collection) {
	fmt.Println("Removing", m["_id"])
	collection.Remove(mongo.M{"_id": m["_id"]})
}