goext/wmo/queryDelete.go

36 lines
1.0 KiB
Go
Raw Normal View History

2023-06-06 21:33:49 +02:00
package wmo
2023-06-07 10:42:56 +02:00
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
2023-08-21 15:08:35 +02:00
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
2023-06-07 10:42:56 +02:00
)
2023-06-08 16:24:53 +02:00
func (c *Coll[TData]) DeleteOneByID(ctx context.Context, id EntityID) error {
2023-06-06 21:33:49 +02:00
_, err := c.coll.DeleteOne(ctx, bson.M{"_id": id})
if err != nil {
2023-08-21 15:08:35 +02:00
return exerr.Wrap(err, "mongo-query[delete-one-by-id] failed").Id("id", id).Str("collection", c.Name()).Build()
2023-06-06 21:33:49 +02:00
}
return nil
}
2023-06-08 16:24:15 +02:00
func (c *Coll[TData]) DeleteOne(ctx context.Context, filterQuery bson.M) error {
_, err := c.coll.DeleteOne(ctx, filterQuery)
if err != nil {
2023-08-21 15:08:35 +02:00
return exerr.Wrap(err, "mongo-query[delete-one] failed").Any("filterQuery", filterQuery).Str("collection", c.Name()).Build()
2023-06-08 16:24:15 +02:00
}
return nil
}
2023-06-06 21:33:49 +02:00
func (c *Coll[TData]) DeleteMany(ctx context.Context, filterQuery bson.M) (*mongo.DeleteResult, error) {
res, err := c.coll.DeleteMany(ctx, filterQuery)
if err != nil {
2023-08-21 15:08:35 +02:00
return nil, exerr.Wrap(err, "mongo-query[delete-many] failed").Any("filterQuery", filterQuery).Str("collection", c.Name()).Build()
2023-06-06 21:33:49 +02:00
}
return res, nil
}