-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (74 loc) · 1.85 KB
/
main.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
/*
* @Descripttion:
* @version:
* @Author: 江小凡
* @Date: 2022-01-26 22:39:39
* @LastEditTime: 2022-03-09 22:30:54
*/
package main
import (
"fmt"
"mongo/conf"
"mongo/crud"
"mongo/log"
)
var dao crud.Crud
type doc struct {
Name string `bson:"name"`
Age int `bson:"age"`
}
func main() {
var err error
fmt.Println(conf.LOG_LEVEL)
log.Log.Infof("----- start mongo -----")
dao = crud.NewCrud()
f, err := dao.CheckExist(conf.CollA, fmt.Sprintf(`{"name":"%s"}`, "江小凡"))
if err != nil {
log.Log.Error("check exist error:", err)
}
fmt.Println("文档存在:", f)
var d doc
var docs []interface{}
d.Name = "江小凡"
d.Age = 18
docs = append(docs, d)
dao.Create(conf.CollA, docs)
var lp *crud.SMongoListParams
lp.Filter = fmt.Sprintf(`{"name":"%s"}`, "江小凡")
lp.Options.Sorts = fmt.Sprintf(`{"age":%d}`, 1)
lp.Options.Limit = 1
lp.Options.Fields = `{"name":1}`
lp.Options.Skip = 0
res, err := dao.List(conf.CollA, lp)
if err != nil {
fmt.Println(err)
}
fmt.Println("文档数量", res.Count, "文档", res.Data)
var gp *crud.SMongoGetParams
gp.Filter = fmt.Sprintf(`{"name":"%s"}`, "江小凡")
gp.Options.Sorts = fmt.Sprintf(`{"age":%d}`, 1)
gp.Options.Skip = 0
gp.Options.Fields = `{"name":1}`
res2, err := dao.Get(conf.CollA, gp)
if err != nil {
fmt.Println(err)
}
fmt.Println("获取单个文档", res2)
var up *crud.SMongoUpdateParams
up.Filter = fmt.Sprintf(`{"name":"%s"}`, "江小凡")
up.Update = fmt.Sprintf(`{"$set":{"age":%d}}`, 19)
up.Options.Multi = true
res3, err := dao.Update(conf.CollA, up)
if err != nil {
fmt.Println(err)
}
fmt.Println("更新文档", res3)
var dp *crud.SMongoDeleteParams
dp.Filter = fmt.Sprintf(`{"name":"%s"}`, "江小凡")
dp.Options.Multi = true
res4, err := dao.Delete(conf.CollA, dp)
if err != nil {
fmt.Println(err)
}
fmt.Println("删除文档", res4)
}