-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_update.go
executable file
·58 lines (49 loc) · 1.54 KB
/
find_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
package mongodb
import (
"context"
"errors"
"reflect"
"time"
// Packages
trace "github.com/mutablelogic/go-accessory/pkg/trace"
bson "go.mongodb.org/mongo-driver/bson"
driver "go.mongodb.org/mongo-driver/mongo"
options "go.mongodb.org/mongo-driver/mongo/options"
// Namespace imports
. "github.com/djthorpe/go-errors"
. "github.com/mutablelogic/go-accessory"
)
///////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// FindUpdate selects a single document based on filter and sort parameters,
// updates the document with the given values and returns the updated document.
func (collection *collection) FindUpdate(ctx context.Context, patch any, sort Sort, filter ...Filter) (any, error) {
// Check for collection
if collection.Collection == nil {
return nil, ErrOutOfOrder
}
// Trace
ctx, matched, _ := trace.WithCollection(ctx, trace.OpFindUpdate, collection.Database().Name(), collection.Name())
defer trace.Do(ctx, collection.traceFn, time.Now())
// Execute operation
result := collection.Collection.FindOneAndUpdate(ctx, and(filter...), bson.D{{"$set", patch}}, &options.FindOneAndUpdateOptions{
Sort: sortdoc(sort),
})
// Check for errors
if err := result.Err(); err != nil {
if errors.Is(result.Err(), driver.ErrNoDocuments) {
return nil, ErrNotFound
} else {
return nil, err
}
} else {
*matched = 1
}
// Create a new document
doc := reflect.New(collection.meta.Type).Interface()
if err := result.Decode(doc); err != nil {
return nil, err
} else {
return doc, nil
}
}