-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
176 lines (159 loc) · 3.48 KB
/
app.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* @Author: DollarKiller
* @Description:
* @Github: https://github.com/dollarkillerx
* @Date: Create in 16:21 2019-10-28
*/
package main
import (
"context"
"fmt"
"github.com/dollarkillerx/mongo"
"github.com/dollarkillerx/mongo/clog"
"github.com/dollarkillerx/mongo/mongo-driver/bson"
"github.com/dollarkillerx/mongo/mongo-driver/bson/primitive"
"log"
"time"
)
type Book struct {
Id primitive.ObjectID `bson:"_id"`
Name string
Category string
Weight int
Author AuthorInfo
}
type AuthorInfo struct {
Name string
Country string
}
const (
categoryComputer = "计算机"
categorySciFi = "科幻"
countryChina = "中国"
countryAmerica = "美国"
)
var (
books = []interface{}{
&Book{
Id: primitive.NewObjectID(),
Name: "深入理解计算机操作系统",
Category: categoryComputer,
Weight: 1,
Author: AuthorInfo{
Name: "兰德尔 E.布莱斯特",
Country: countryAmerica,
},
},
&Book{
Id: primitive.NewObjectID(),
Name: "深入理解Linux内核",
Category: categoryComputer,
Weight: 1,
Author: AuthorInfo{
Name: "博韦,西斯特",
Country: countryAmerica,
},
},
&Book{
Id: primitive.NewObjectID(),
Name: "三体",
Category: categorySciFi,
Weight: 1,
Author: AuthorInfo{
Name: "刘慈欣",
Country: countryChina,
},
},
}
)
func main() {
uri := "mongodb://127.0.0.1:27017"
db, e := mongo.Open(uri)
if e != nil {
panic(e)
}
e = db.Ping()
if e != nil {
panic(e)
}
clog.Println("MongoDb 链接成功")
// 配置
db.SetMaxOpenConn(1)
db.SetConnMaxLifetime(400 * time.Millisecond)
// 创建读取
collection := db.NewCollection("test", "os")
// 空气数据库
e = collection.Drop()
if e != nil {
log.Fatalln(e)
}
// 设置索引
idxRet, e := collection.CreatIndex("name", 1)
if e != nil {
log.Fatalln(e)
}
log.Println("Collection.Indexes().CreateOne:", idxRet)
// 插入一条数据
insertOneResult, err := collection.InsertOne(books[0])
if err != nil {
log.Fatal(err)
}
log.Println("Collection.InsertOne:", insertOneResult.InsertedID)
// 插入多条数据
insertManyResult, err := collection.InsertMany(books[1:])
if err != nil {
log.Fatal(err)
}
log.Println("collection.InsertMany:", insertManyResult.InsertedIDs)
// 获取数据总数
count, err := collection.CountDocuments()
if err != nil {
log.Fatal(count)
}
log.Println("collection.CountDocuments:", count)
// 查询单挑数据
var one Book
e = collection.FindOne(context.TODO(), bson.M{"name": "三体"}).Decode(&one)
if e != nil {
log.Fatal(err)
}
log.Println("collection.FindOne: ", one)
// 查询多条数据 方式一
cur, err := collection.Find(context.Background(), bson.D{})
if err != nil {
log.Fatal(err)
}
if err := cur.Err(); err != nil {
log.Fatal(err)
}
var all []*Book
err = cur.All(context.TODO(), &all)
if err != nil {
log.Fatal(err)
}
e = cur.Close(context.TODO())
if e != nil {
panic(e)
}
log.Println("collection.Find curl.All", all)
for _, one := range all {
fmt.Println(one)
}
//// 查询多条数据 方式二
//cur, err = collection.Find(context.Background(), bson.D{})
//if err != nil {
// log.Fatal(err)
//}
//if err := cur.Err(); err != nil {
// log.Fatal(err)
//}
//for cur.Next(context.TODO()) {
// var b Book
// if err = cur.Decode(&b); err != nil {
// log.Fatal(err)
// }
// clog.Println("collection.Find cur.Next:")
// clog.Println(b)
//}
//cur.Close(context.TODO())
}