2023-06-06 21:33:49 +02:00
|
|
|
package wmo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
2023-06-07 10:56:11 +02:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2023-06-06 21:33:49 +02:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2023-06-07 10:56:11 +02:00
|
|
|
func (c *Coll[TData]) UpdateMany(ctx context.Context, filterQuery bson.M, updateQuery bson.M) (*mongo.UpdateResult, error) {
|
|
|
|
res, err := c.coll.UpdateMany(ctx, filterQuery, updateQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-06-08 16:24:15 +02:00
|
|
|
func (c *Coll[TData]) ReplaceOne(ctx context.Context, filterQuery bson.M, value TData) error {
|
|
|
|
_, err := c.coll.UpdateOne(ctx, filterQuery, bson.M{"$set": value})
|
2023-06-06 21:33:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-06 21:40:34 +02:00
|
|
|
|
2023-06-08 16:24:15 +02:00
|
|
|
func (c *Coll[TData]) FindOneAndReplace(ctx context.Context, filterQuery bson.M, value TData) (TData, error) {
|
2023-06-06 21:40:34 +02:00
|
|
|
var res TData
|
|
|
|
|
2023-06-08 16:24:15 +02:00
|
|
|
err := c.coll.FindOneAndUpdate(ctx, filterQuery, bson.M{"$set": value}, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&res)
|
2023-06-06 21:40:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return *new(TData), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|