-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.go
More file actions
135 lines (121 loc) · 3.54 KB
/
Copy pathupdate.go
File metadata and controls
135 lines (121 loc) · 3.54 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package morm
import (
"context"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// ToUpdateStruct converts a struct to a primitive.M for MongoDB update operations.
// It marshals the input struct to BSON and then unmarshals it to a primitive.M.
//
// Parameters:
// - data: The struct to be converted.
//
// Returns:
// - primitive.M: The converted BSON data.
// - error: An error if the conversion fails.
//
// Example:
//
// type UpdateData struct {
// Field1 string `bson:"field1"`
// Field2 int `bson:"field2"`
// }
//
// data := UpdateData{"value1", 42}
// converted, err := ToUpdateStruct(data)
// if err != nil {
// // Handle error
// }
//
// This function is useful for converting a struct to the format required for MongoDB update operations.
func ToUpdateStruct(data interface{}) (primitive.M, error) {
// Marshal the struct to BSON
bsonData, err := bson.Marshal(data)
if err != nil {
return nil, err
}
// Unmarshal the BSON to primitive.M
var result primitive.M
err = bson.Unmarshal(bsonData, &result)
if err != nil {
return nil, err
}
return result, nil
}
// UpdateOne updates a single document in the specified collection based on the filter and update parameters.
//
// Parameters:
// - filter: The filter criteria to identify the document to update.
// - update: The update data to be applied to the document.
//
// Returns:
// - error: An error if the update operation fails.
//
// Example:
//
// filter := bson.D{{"name", "John"}}
// update := bson.D{{"$set", bson.D{{"age", 30}}}}
// err := qb.UpdateOne(filter, update)
// if err != nil {
// // Handle error
// }
//
// This method is useful for updating a single document in a MongoDB collection.
func (qb *CollectQueryBuilder) UpdateOne(filter interface{}, update interface{}) error {
collection := qb.c.collection
convertedUpdate, _ := ToUpdateStruct(update)
// Set updatedAt field to the current time
updateWithUpdatedAt := bson.M{
"$set": bson.M{
"updatedAt": time.Now(),
},
}
// Merge the provided update with the update for updatedAt
for key, value := range convertedUpdate {
updateWithUpdatedAt["$set"].(bson.M)[key] = value
}
// Perform the update
_, err := collection.UpdateOne(context.Background(), filter, updateWithUpdatedAt)
return err
}
// Update updates multiple documents in the specified collection based on the filter and update parameters.
//
// Parameters:
// - filter: The filter criteria to identify the documents to update.
// - update: The update data to be applied to the documents.
// - ctx: Optional context for the MongoDB operation.
//
// Returns:
// - error: An error if the update operation fails.
//
// Example:
//
// filter := bson.D{{"status", "pending"}}
// update := bson.D{{"$set", bson.D{{"status", "processed"}}}}
// err := qb.Update(filter, update)
// if err != nil {
// // Handle error
// }
//
// This method is useful for updating multiple documents in a MongoDB collection.
func (qb *CollectQueryBuilder) Update(filter interface{}, update interface{}, ctx ...context.Context) error {
collection := qb.c.collection
var backgroundContext = context.Background()
if len(ctx) > 0 {
backgroundContext = ctx[0]
}
// Set updatedAt field to the current time
updateWithUpdatedAt := bson.M{
"$set": bson.M{
"updatedAt": time.Now(),
},
}
// Merge the provided update with the update for updatedAt
updateWithUpdatedAt["$set"].(bson.M)["$set"] = update
_, err := collection.UpdateMany(backgroundContext, filter, updateWithUpdatedAt)
if err != nil {
return err
}
return nil
}