Use update operator not the update pipeline

v2
Nikita Tokarchuk 4 years ago
parent eeb1a8d598
commit fc2a867cbb
No known key found for this signature in database
GPG Key ID: DBFD964471BAE65C
  1. 1
      go.mod
  2. 2
      go.sum
  3. 22
      mongox/database/updateone.go
  4. 29
      mongox/query/query.go
  5. 6
      mongox/query/update.go

@ -6,5 +6,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 github.com/modern-go/reflect2 v1.0.1
github.com/stretchr/testify v1.6.1 github.com/stretchr/testify v1.6.1
github.com/valyala/bytebufferpool v1.0.0
go.mongodb.org/mongo-driver v1.4.3 go.mongodb.org/mongo-driver v1.4.3
) )

@ -82,6 +82,8 @@ github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc h1:n+nNi93yXLkJvKwXNP9d55HC7lGK4H/SRcwB5IaUZLo= github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc h1:n+nNi93yXLkJvKwXNP9d55HC7lGK4H/SRcwB5IaUZLo=

@ -1,8 +1,6 @@
package database package database
import ( import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
@ -18,10 +16,14 @@ func (d *Database) UpdateOne(target interface{}, filters ...interface{}) (err er
return return
} }
updaterDoc, err := composed.Updater()
if err != nil {
return
}
collection := d.GetCollectionOf(target) collection := d.GetCollectionOf(target)
protected := base.GetProtection(target) protected := base.GetProtection(target)
ctx := query.WithContext(d.Context(), composed) ctx := query.WithContext(d.Context(), composed)
updater := composed.Updater()
opts := options.FindOneAndUpdate() opts := options.FindOneAndUpdate()
opts.SetReturnDocument(options.After) opts.SetReturnDocument(options.After)
@ -30,12 +32,12 @@ func (d *Database) UpdateOne(target interface{}, filters ...interface{}) (err er
if !protected.X.IsZero() { if !protected.X.IsZero() {
query.Push(composed, protected) query.Push(composed, protected)
} }
updater = append(updater, primitive.M{ //updater = append(updater, primitive.M{
"$set": primitive.M{ // "$set": primitive.M{
"_x": primitive.NewObjectID(), // "_x": primitive.NewObjectID(),
"_v": time.Now().Unix(), // "_v": time.Now().Unix(),
}, // },
}) //})
} }
defer func() { defer func() {
@ -47,7 +49,7 @@ func (d *Database) UpdateOne(target interface{}, filters ...interface{}) (err er
return return
}() }()
result := collection.FindOneAndUpdate(ctx, composed.M(), updater, opts) result := collection.FindOneAndUpdate(ctx, composed.M(), updaterDoc, opts)
if result.Err() != nil { if result.Err() != nil {
return result.Err() return result.Err()
} }

@ -1,6 +1,9 @@
package query package query
import ( import (
"github.com/modern-go/reflect2"
"github.com/valyala/bytebufferpool"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
@ -66,13 +69,35 @@ func (q *Query) Skipper() (skip *int64) {
} }
// Updater is an update command for a query // Updater is an update command for a query
func (q *Query) Updater() (update primitive.A) { func (q *Query) Updater() (update primitive.M, err error) {
if q.updater == nil { if q.updater == nil {
update = primitive.M{}
return return
} }
return q.updater.Update() update = q.updater.Update()
if reflect2.IsNil(update) {
update = primitive.M{}
return
}
buffer := bytebufferpool.Get()
defer bytebufferpool.Put(buffer)
// convert update document to bson map values
bsonBytes, err := bson.MarshalAppend(buffer.B, update)
if err != nil {
return
}
update = primitive.M{}
err = bson.Unmarshal(bsonBytes, update)
if err != nil {
return
}
return
} }
// Preloader is a preloader list for a query // Preloader is a preloader list for a query

@ -6,7 +6,7 @@ import (
// Updater is a filter to update the data // Updater is a filter to update the data
type Updater interface { type Updater interface {
Update() (update primitive.A) Update() (update primitive.M)
} }
// Update is a simple implementations of the Updater filter // Update is a simple implementations of the Updater filter
@ -15,6 +15,6 @@ type Update primitive.M
var _ Updater = &Update{} var _ Updater = &Update{}
// Update returns an update command // Update returns an update command
func (u Update) Update() (update primitive.A) { func (u Update) Update() (update primitive.M) {
return primitive.A{primitive.M(u)} return primitive.M(u)
} }

Loading…
Cancel
Save