This commit is contained in:
2023-07-17 12:42:49 +02:00
parent 2f01a1d50f
commit c320bb3d90
2 changed files with 20 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ package wmo
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
@@ -36,3 +37,20 @@ func (c *Coll[TData]) AggregateOneOpt(ctx context.Context, pipeline mongo.Pipeli
return nil, nil
}
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")
}