/
update.go
128 lines (109 loc) · 2.67 KB
/
update.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"
"github.com/pkg/errors"
"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"
)
type UpdateOneObject struct {
ctx context.Context
coll *mongo.Collection
opts *options.UpdateOptions
filter interface{}
updater interface{}
}
func (o *UpdateOneObject) Where(filter interface{}) *UpdateOneObject {
o.filter = filter
return o
}
func (o *UpdateOneObject) Upsert() *UpdateOneObject {
o.opts.SetUpsert(true)
return o
}
func (o *UpdateOneObject) Set(doc interface{}) *UpdateOneObject {
var updater bson.M
if o.updater == nil {
updater = bson.M{}
} else {
updater = o.updater.(bson.M)
}
updater["$set"] = doc
o.updater = updater
return o
}
func (o *UpdateOneObject) Unset(doc interface{}) *UpdateOneObject {
var updater bson.M
if o.updater == nil {
updater = bson.M{}
} else {
updater = o.updater.(bson.M)
}
updater["$unset"] = doc
o.updater = updater
return o
}
func (o *UpdateOneObject) Pull(doc interface{}) *UpdateOneObject {
var updater bson.M
if o.updater == nil {
updater = bson.M{}
} else {
updater = o.updater.(bson.M)
}
updater["$pull"] = doc
o.updater = updater
return o
}
func (o *UpdateOneObject) Updater(doc interface{}) *UpdateOneObject {
o.updater = doc
return o
}
// warning: 如果没有发生insert(upsert时,已经存在匹配的记录),则不会返回ID
func (o *UpdateOneObject) Do() (string, error) {
if o.filter == nil {
o.filter = bson.D{}
}
rst, err := o.coll.UpdateOne(o.ctx, o.filter, o.updater, o.opts)
// log.Printf("rst=%+v, err:%+v", rst, err)
if err != nil {
return "", errors.Wrapf(err,
"error happened on call mongo api: filter:%v, updater:%v", o.filter, o.updater)
}
if id, ok := rst.UpsertedID.(primitive.ObjectID); ok {
return id.Hex(), err
}
if id, ok := rst.UpsertedID.(string); ok {
return id, err
}
return "", err
}
type UpdateManyObject struct {
ctx context.Context
coll *mongo.Collection
opts *options.UpdateOptions
filter interface{}
updater interface{}
}
func (o *UpdateManyObject) Upsert() *UpdateManyObject {
o.opts.SetUpsert(true)
return o
}
func (o *UpdateManyObject) Where(filter interface{}) *UpdateManyObject {
o.filter = filter
return o
}
func (o *UpdateManyObject) Set(doc interface{}) *UpdateManyObject {
o.updater = bson.M{"$set": doc}
return o
}
func (o *UpdateManyObject) Updater(doc interface{}) *UpdateManyObject {
o.updater = doc
return o
}
func (o *UpdateManyObject) Do() (*mongo.UpdateResult, error) {
if o.filter == nil {
o.filter = bson.D{}
}
return o.coll.UpdateMany(o.ctx, o.filter, o.updater, o.opts)
}