-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
42 lines (29 loc) · 928 Bytes
/
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
package mongogo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func (m *Mongo[T]) Update(query, data interface{}, filter string) (*T, error) {
collection := m.GetCollection()
_, err := collection.UpdateOne(context.Background(), query, data)
if err != nil {
return nil, err
}
return m.FindOne(query, filter)
}
func (m *Mongo[T]) UpdateOneWithReplace(query, data interface{}, filter string) (*T, error) {
collection := m.GetCollection()
_, err := collection.ReplaceOne(context.Background(), query, data)
if err != nil {
return nil, err
}
return m.FindOne(query, filter)
}
func (m *Mongo[T]) UpdateOneWithReplaceById(id string, data interface{}, filter string) (*T, error) {
objectId, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
return m.UpdateOneWithReplace(bson.D{{Key: "_id", Value: objectId}}, data, filter)
}