|
|
|
@ -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() |
|
|
|
|
} |
|
|
|
|