forked from agtorre/go-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
40 lines (31 loc) · 694 Bytes
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package mongodb
import (
"fmt"
"gopkg.in/mgo.v2/bson"
)
// State is our data model
type State struct {
Name string `bson:"name"`
Population int `bson:"pop"`
}
// Exec creates then queries an Example
func Exec() error {
db, err := Setup()
if err != nil {
return err
}
conn := db.DB("gocookbook").C("example")
// we can inserts many rows at once
if err := conn.Insert(&State{"Washington", 7062000}, &State{"Oregon", 3970000}); err != nil {
return err
}
var s State
if err := conn.Find(bson.M{"name": "Washington"}).One(&s); err != nil {
return err
}
if err := conn.DropCollection(); err != nil {
return err
}
fmt.Printf("State: %#v\n", s)
return nil
}