mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-22 15:53:36 +00:00
Use update operator not the update pipeline
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user