-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_query.go
More file actions
37 lines (32 loc) · 1.08 KB
/
replace_query.go
File metadata and controls
37 lines (32 loc) · 1.08 KB
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
package easymongo
import "go.mongodb.org/mongo-driver/mongo/options"
// ReplaceQuery is a helper for replacement query actions and options.
type ReplaceQuery struct {
newObj interface{}
*Query
}
// Replace returns a ReplaceQuery. Trying running `.One()` against this.
// This is used to replace an entire object. If you are looking to update just part of a document
// (e.g. $set a field, $inc a counter up or down, etc.) you should instead use collection.Update().One().
func (c *Collection) Replace(filter interface{}, obj interface{}) *ReplaceQuery {
return &ReplaceQuery{
newObj: obj,
Query: c.query(filter),
}
}
// Execute runs the ReplaceQuery. No actions are taken until this query is run.
func (rq *ReplaceQuery) One() error {
// var result *mongo.UpdateResult
opts := options.Replace()
// TODO: ReplaceOptions
ctx, cancelFunc := rq.getContext()
defer cancelFunc()
res, err := rq.collection.mongoColl.ReplaceOne(ctx, rq.filter, rq.newObj, opts)
if err != nil {
return err
}
err = rq.collection.handleErr(err)
// TODO: ReplaceQuery ErrNotFound behavior
_ = res
return nil
}