goext/wmo/queryFind.go

63 lines
1.4 KiB
Go
Raw Normal View History

2023-06-06 21:33:49 +02:00
package wmo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func (c *Coll[TData]) FindOne(ctx context.Context, filter bson.M) (TData, error) {
2023-06-10 16:22:14 +02:00
mongoRes := c.coll.FindOne(ctx, filter)
2023-06-06 21:33:49 +02:00
2023-06-10 16:22:14 +02:00
return c.decodeSingle(ctx, mongoRes)
2023-06-06 21:33:49 +02:00
}
func (c *Coll[TData]) FindOneOpt(ctx context.Context, filter bson.M) (*TData, error) {
2023-06-10 16:22:14 +02:00
mongoRes := c.coll.FindOne(ctx, filter)
2023-06-06 21:33:49 +02:00
2023-06-10 16:22:14 +02:00
res, err := c.decodeSingle(ctx, mongoRes)
2023-06-06 21:33:49 +02:00
if err == mongo.ErrNoDocuments {
return nil, nil
}
if err != nil {
return nil, err
}
return &res, nil
}
func (c *Coll[TData]) FindOneByID(ctx context.Context, id EntityID) (TData, error) {
2023-06-10 16:22:14 +02:00
mongoRes := c.coll.FindOne(ctx, bson.M{"_id": id})
2023-06-06 21:33:49 +02:00
2023-06-10 16:22:14 +02:00
return c.decodeSingle(ctx, mongoRes)
2023-06-06 21:33:49 +02:00
}
func (c *Coll[TData]) FindOneOptByID(ctx context.Context, id EntityID) (*TData, error) {
2023-06-10 16:22:14 +02:00
mongoRes := c.coll.FindOne(ctx, bson.M{"_id": id})
2023-06-06 21:33:49 +02:00
2023-06-10 16:22:14 +02:00
res, err := c.decodeSingle(ctx, mongoRes)
2023-06-06 21:33:49 +02:00
if err == mongo.ErrNoDocuments {
return nil, nil
}
if err != nil {
return nil, err
}
return &res, nil
}
func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options.FindOptions) ([]TData, error) {
cursor, err := c.coll.Find(ctx, filter, opts...)
if err != nil {
return nil, err
}
2023-06-10 16:22:14 +02:00
res, err := c.decodeAll(ctx, cursor)
2023-06-06 21:33:49 +02:00
if err != nil {
return nil, err
}
return res, nil
}