-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.go
173 lines (169 loc) · 4.62 KB
/
write.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
package mongo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"reflect"
"strings"
)
func MakeBsonMap(modelType reflect.Type) map[string]string {
maps := make(map[string]string)
numField := modelType.NumField()
for i := 0; i < numField; i++ {
field := modelType.Field(i)
key1 := field.Name
if tag0, ok0 := field.Tag.Lookup("json"); ok0 {
if strings.Contains(tag0, ",") {
a := strings.Split(tag0, ",")
key1 = a[0]
} else {
key1 = tag0
}
}
if tag, ok := field.Tag.Lookup("bson"); ok {
if tag != "-" {
if strings.Contains(tag, ",") {
a := strings.Split(tag, ",")
if key1 == "-" {
key1 = a[0]
}
maps[key1] = a[0]
} else {
if key1 == "-" {
key1 = tag
}
maps[key1] = tag
}
}
} else {
if key1 == "-" {
key1 = field.Name
}
maps[key1] = key1
}
}
return maps
}
func InsertOne(ctx context.Context, collection *mongo.Collection, model interface{}) (*primitive.ObjectID, int64, error) {
result, err := collection.InsertOne(ctx, model)
if err != nil {
errMsg := err.Error()
if strings.Contains(errMsg, "duplicate key error collection:") {
return nil, 0, nil
} else {
return nil, 0, err
}
} else {
if idValue, ok := result.InsertedID.(primitive.ObjectID); ok {
return &idValue, 1, nil
}
return nil, 1, nil
}
}
// For Patch
func MapToBson(object map[string]interface{}, objectMap map[string]string) map[string]interface{} {
result := make(map[string]interface{})
for key, value := range object {
field, ok := objectMap[key]
if ok {
result[field] = value
}
}
return result
}
func PatchOne(ctx context.Context, collection *mongo.Collection, id interface{}, model map[string]interface{}) (int64, error) {
filter := bson.M{"_id": id}
updateQuery := bson.M{
"$set": model,
}
result, err := collection.UpdateOne(ctx, filter, updateQuery)
if err != nil {
return 0, err
}
if result.ModifiedCount > 0 {
return result.ModifiedCount, err
} else if result.UpsertedCount > 0 {
return result.UpsertedCount, err
} else {
return result.MatchedCount, err
}
}
func PatchOneByFilter(ctx context.Context, collection *mongo.Collection, filter bson.D, model map[string]interface{}) (int64, error) { //Patch
updateQuery := bson.M{
"$set": model,
}
result, err := collection.UpdateOne(ctx, filter, updateQuery)
if result.ModifiedCount > 0 {
return result.ModifiedCount, err
} else if result.UpsertedCount > 0 {
return result.UpsertedCount, err
} else {
return result.MatchedCount, err
}
}
func UpdateOne(ctx context.Context, collection *mongo.Collection, id interface{}, model interface{}) (int64, error) { //Patch
filter := bson.M{"_id": id}
updateQuery := bson.M{
"$set": model,
}
result, err := collection.UpdateOne(ctx, filter, updateQuery)
if result.ModifiedCount > 0 {
return result.ModifiedCount, err
} else if result.UpsertedCount > 0 {
return result.UpsertedCount, err
} else {
return result.MatchedCount, err
}
}
func UpdateOneByFilter(ctx context.Context, collection *mongo.Collection, filter bson.D, model interface{}) (int64, error) { //Patch
updateQuery := bson.M{
"$set": model,
}
result, err := collection.UpdateOne(ctx, filter, updateQuery)
if result.ModifiedCount > 0 {
return result.ModifiedCount, err
} else if result.UpsertedCount > 0 {
return result.UpsertedCount, err
} else {
return result.MatchedCount, err
}
}
func UpsertOne(ctx context.Context, collection *mongo.Collection, id interface{}, model interface{}) (int64, error) {
filter := bson.M{"_id": id}
updateQuery := bson.M{
"$set": model,
}
opts := options.Update().SetUpsert(true)
res, err := collection.UpdateOne(ctx, filter, updateQuery, opts)
if res.ModifiedCount > 0 {
return res.ModifiedCount, err
} else if res.UpsertedCount > 0 {
return res.UpsertedCount, err
} else {
return res.MatchedCount, err
}
}
func UpsertOneByFilter(ctx context.Context, collection *mongo.Collection, filter bson.D, model interface{}) (int64, error) {
updateQuery := bson.M{
"$set": model,
}
opts := options.Update().SetUpsert(true)
res, err := collection.UpdateOne(ctx, filter, updateQuery, opts)
if res.ModifiedCount > 0 {
return res.ModifiedCount, err
} else if res.UpsertedCount > 0 {
return res.UpsertedCount, err
} else {
return res.MatchedCount, err
}
}
func DeleteOne(ctx context.Context, collection *mongo.Collection, id interface{}) (int64, error) {
filter := bson.M{"_id": id}
result, err := collection.DeleteOne(ctx, filter)
if result == nil {
return 0, err
}
return result.DeletedCount, err
}