-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
50 lines (44 loc) · 897 Bytes
/
db.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
/**
* @Author: DollarKiller
* @Description:
* @Github: https://github.com/dollarkillerx
* @Date: Create in 10:29 2019-10-29
*/
package main
import (
"context"
"github.com/dollarkillerx/mongo"
"github.com/dollarkillerx/mongo/clog"
"log"
"time"
)
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)
database := db.Database("okp")
collection := database.Collection("BOOK")
getCollection, pul, e := collection.GetCollection()
if e == nil {
defer func() {
err := collection.PulCollection(pul)
if err == nil {
log.Println("放回成功")
}
}()
}
e = getCollection.Drop(context.TODO())
if e == nil {
log.Println("数据库清空成功200ok")
}
}