/
student.go
128 lines (102 loc) · 2.38 KB
/
student.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
package mongo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Student struct {
Id int64 `bson:"id"`
ClassId int64 `bson:"score,omitempty"`
Name string `bson:"name,omitempty"`
}
var (
collection *mongo.Collection
)
func Init() {
ctx := context.Background()
uri := "mongo://username:password@localhost:27017"
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
panic(err)
return
}
collection = client.Database("database_name").Collection("collection_name")
}
func Insert(ctx context.Context, student Student) (err error) {
_, err = collection.InsertOne(ctx, &student)
return
}
func Update(ctx context.Context, student Student) (modified int64, err error) {
filter := bson.M{
"id": student.Id,
}
var result *mongo.UpdateResult
result, err = collection.UpdateOne(ctx, filter, bson.M{"$set": &student})
if err != nil {
panic(err)
return
}
if result != nil {
modified = result.ModifiedCount
}
return
}
func Delete(ctx context.Context, student Student) (deleted int64, err error) {
filter := bson.M{
"id": student.Id,
}
var result *mongo.DeleteResult
result, err = collection.DeleteMany(ctx, filter)
if err != nil {
panic(err)
return
}
if result != nil {
deleted = result.DeletedCount
}
return
}
// ReplaceOne
// 未命中不报错, modified = 0
func ReplaceOne(ctx context.Context, student Student) (modified int64, err error) {
// 一般情况可以使用非 bson.M{}
filter := map[string]interface{}{
"id": student.Id,
}
var result *mongo.UpdateResult
result, err = collection.ReplaceOne(ctx, filter, student)
if err != nil {
panic(err)
return
}
if result != nil {
modified = result.ModifiedCount
}
return
}
// FindByFilter 根据条件查找记录
// 注 : filter 需要大于 0
// 未命中不报错, len(userComments) = 0
func FindByFilter(ctx context.Context, filter map[string]interface{}) (students []Student, err error) {
// 参数集无效, 不允许全量查
if len(filter) == 0 {
return
}
var cursor *mongo.Cursor
cursor, err = collection.Find(ctx, filter)
if err != nil {
panic(err)
return
}
// 遍历结果
for cursor.Next(ctx) {
var student = Student{}
if err = cursor.Decode(&student); err != nil {
panic(err)
return
}
students = append(students, student)
}
return
}