-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateone.go
51 lines (41 loc) · 1.74 KB
/
updateone.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
package mongohelper
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// UpdateOne wraps the mongo.Database.Collection.UpdateOne() method
// It returns the number of matched records and an error
// The filter parameter must be a document containing query operators and can be used to select the document to be
// updated. It cannot be nil. If the filter does not match any documents, the operation will succeed and an UpdateResult
// with a MatchedCount of 0 will be returned. If the filter matches multiple documents, one will be selected from the
// matched set and MatchedCount will equal 1.
//
// The update parameter must be a document containing update operators
// (https://docs.mongodb.com/manual/reference/operator/update/) and can be used to specify the modifications to be
// made to the selected document. It cannot be nil or empty.
func (l *Link) UpdateOne(database, collection string, filter, update interface{}) (int64, error) {
if err := l.linkCheck("link.UpdateOne"); err != nil {
return 0, err
}
ctx, cancel := context.WithTimeout(context.Background(), l.execTimeout())
defer cancel()
rs, err := l.client.Database(database).Collection(collection).UpdateOne(ctx, filter, update, options.Update())
if err != nil {
// If not connected, try once again
if errors.Is(err, mongo.ErrClientDisconnected) {
if err = l.connect(); err != nil {
return 0, err
}
ctx2, cancel2 := context.WithTimeout(context.Background(), l.execTimeout())
defer cancel2()
if rs, err = l.client.Database(database).Collection(collection).UpdateOne(ctx2, filter, update, options.Update()); err != nil {
return 0, err
}
} else {
return 0, err
}
}
return rs.MatchedCount, nil
}