-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_query.go
100 lines (89 loc) · 3.08 KB
/
update_query.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
package easymongo
import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// UpdateQuery helps construct and execute update queries
type UpdateQuery struct {
updateQuery interface{}
upsert *bool
bypassDocumentValidation *bool
arrayFilters *options.ArrayFilters
*Query
}
// Update returns an UpdateQuery object which can be actioned upon by calling One() or Many()
// e.g. c.Update().Many()
func (c *Collection) Update(filter interface{}, update interface{}) *UpdateQuery {
return &UpdateQuery{
updateQuery: update,
Query: c.query(filter),
}
}
// ArrayFilters is used to hold filters for the array filters CRUD option. If a registry is nil, bson.DefaultRegistry
// will be used when converting the filter interfaces to BSON.
// TODO: ArrayFilters helpers
func (uq *UpdateQuery) ArrayFilters(o *options.ArrayFilters) *UpdateQuery {
uq.arrayFilters = o
return uq
}
// Upsert specifies that if a document doesn't exist that matches the update filter,
// then a new document will be created as a result of this query run.
func (uq *UpdateQuery) Upsert() *UpdateQuery {
t := true
uq.upsert = &t
return uq
}
// TODO: BypassDocumentValidation options docs
func (uq *UpdateQuery) BypassDocumentValidation() *UpdateQuery {
t := true
uq.bypassDocumentValidation = &t
return uq
}
// updateOptions returns the native mongo driver options.UpdateOptions using
// the provided query information.
func (uq *UpdateQuery) updateOptions() *options.UpdateOptions {
o := &options.UpdateOptions{
ArrayFilters: uq.arrayFilters,
BypassDocumentValidation: uq.bypassDocumentValidation,
Collation: uq.collation,
Upsert: uq.upsert,
}
if uq.hintIndices != nil {
o.Hint = *uq.hintIndices
}
return o
}
// One runs the UpdateQuery against the first matching document.
// No actions are taken until this function is called.
func (uq *UpdateQuery) One() (err error) {
var result *mongo.UpdateResult
mongoColl := uq.collection.mongoColl
ctx, cancelFunc := uq.getContext()
defer cancelFunc()
opts := uq.updateOptions()
result, err = mongoColl.UpdateOne(ctx, uq.filter, uq.updateQuery, opts)
// TODO: Handle ErrNotFound
if err == nil && result.MatchedCount == 0 {
// TODO: Inject ErrNotFound
}
err = uq.collection.handleErr(err)
return err
}
// All runs the UpdateQuery against all matching documents.
// A note that the updatedCount includes the count for upserted documents.
// No actions are taken until this function is called.
func (uq *UpdateQuery) All() (matchedCount, updatedCount int, err error) {
var result *mongo.UpdateResult
mongoColl := uq.collection.mongoColl
ctx, cancelFunc := uq.getContext()
defer cancelFunc()
opts := uq.updateOptions()
result, err = mongoColl.UpdateMany(ctx, uq.filter, uq.updateQuery, opts)
err = uq.collection.handleErr(err)
if err == nil && result.MatchedCount == 0 {
// TODO: Inject ErrNotFound
}
matchedCount = int(result.MatchedCount)
updatedCount = int(result.ModifiedCount) + int(result.UpsertedCount)
return matchedCount, updatedCount, err
}