goext/wmo/queryAggregate.go

57 lines
1.2 KiB
Go
Raw Normal View History

2023-06-06 21:33:49 +02:00
package wmo
import (
"context"
2023-07-17 12:42:49 +02:00
"errors"
2023-06-06 21:33:49 +02:00
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func (c *Coll[TData]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, opts ...*options.AggregateOptions) ([]TData, error) {
cursor, err := c.coll.Aggregate(ctx, pipeline, 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
}
2023-06-10 16:28:50 +02:00
func (c *Coll[TData]) AggregateOneOpt(ctx context.Context, pipeline mongo.Pipeline, opts ...*options.AggregateOptions) (*TData, error) {
cursor, err := c.coll.Aggregate(ctx, pipeline, opts...)
if err != nil {
return nil, err
}
if cursor.Next(ctx) {
v, err := c.decodeSingle(ctx, cursor)
if err != nil {
return nil, err
}
return &v, nil
}
return nil, nil
}
2023-07-17 12:42:49 +02:00
func (c *Coll[TData]) AggregateOne(ctx context.Context, pipeline mongo.Pipeline, opts ...*options.AggregateOptions) (TData, error) {
cursor, err := c.coll.Aggregate(ctx, pipeline, opts...)
if err != nil {
return *new(TData), err
}
if cursor.Next(ctx) {
v, err := c.decodeSingle(ctx, cursor)
if err != nil {
return *new(TData), err
}
return v, nil
}
return *new(TData), errors.New("no document in result")
}