This commit is contained in:
2023-06-06 21:33:49 +02:00
parent 555096102a
commit 2ad820be8d
7 changed files with 256 additions and 206 deletions

45
wmo/queryUpdate.go Normal file
View File

@@ -0,0 +1,45 @@
package wmo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
func (c *Coll[TData]) FindOneAndUpdate(ctx context.Context, filterQuery bson.M, updateQuery bson.M) (TData, error) {
var res TData
err := c.coll.FindOneAndUpdate(ctx, filterQuery, updateQuery, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&res)
if err != nil {
return *new(TData), err
}
return res, nil
}
func (c *Coll[TData]) UpdateOne(ctx context.Context, filterQuery bson.M, updateQuery bson.M) error {
_, err := c.coll.UpdateOne(ctx, filterQuery, updateQuery)
if err != nil {
return err
}
return nil
}
func (c *Coll[TData]) UpdateOneByID(ctx context.Context, id EntityID, updateQuery bson.M) error {
_, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, updateQuery)
if err != nil {
return err
}
return nil
}
func (c *Coll[TData]) ReplaceOne(ctx context.Context, id EntityID, value TData) error {
_, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, value)
if err != nil {
return err
}
return nil
}