Use update operator not the update pipeline

This commit is contained in:
2020-11-18 22:30:16 +01:00
parent eeb1a8d598
commit fc2a867cbb
5 changed files with 45 additions and 15 deletions
+12 -10
View File
@@ -1,8 +1,6 @@
package database
import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
@@ -18,10 +16,14 @@ func (d *Database) UpdateOne(target interface{}, filters ...interface{}) (err er
return
}
updaterDoc, err := composed.Updater()
if err != nil {
return
}
collection := d.GetCollectionOf(target)
protected := base.GetProtection(target)
ctx := query.WithContext(d.Context(), composed)
updater := composed.Updater()
opts := options.FindOneAndUpdate()
opts.SetReturnDocument(options.After)
@@ -30,12 +32,12 @@ func (d *Database) UpdateOne(target interface{}, filters ...interface{}) (err er
if !protected.X.IsZero() {
query.Push(composed, protected)
}
updater = append(updater, primitive.M{
"$set": primitive.M{
"_x": primitive.NewObjectID(),
"_v": time.Now().Unix(),
},
})
//updater = append(updater, primitive.M{
// "$set": primitive.M{
// "_x": primitive.NewObjectID(),
// "_v": time.Now().Unix(),
// },
//})
}
defer func() {
@@ -47,7 +49,7 @@ func (d *Database) UpdateOne(target interface{}, filters ...interface{}) (err er
return
}()
result := collection.FindOneAndUpdate(ctx, composed.M(), updater, opts)
result := collection.FindOneAndUpdate(ctx, composed.M(), updaterDoc, opts)
if result.Err() != nil {
return result.Err()
}