-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.go
49 lines (34 loc) · 775 Bytes
/
mongo.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
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"log"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type User struct {
Name string
Age int
}
func main() {
fmt.Println("connect start....")
session , err := mgo.Dial("127.0.0.1:27017")
if err != nil {
panic(err)
}
defer session.Close()
fmt.Println("connect success")
session.SetMode(mgo.Monotonic , true)
c := session.DB("test").C("user")
err = c.Insert(&User{"jin" , 18} ,
&User{"abli" , 19})
if err != nil {
log.Fatal(err)
}
fmt.Println("insert success")
result := User{}
err = c.Find(bson.M{"name": "jin"}).One(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println("user: " , result)
}