From c320bb3d90e578e5aaf3c669e10d13486c4d0a70 Mon Sep 17 00:00:00 2001 From: risqy Date: Mon, 17 Jul 2023 12:42:49 +0200 Subject: [PATCH] v0.0.170 --- goextVersion.go | 4 ++-- wmo/queryAggregate.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/goextVersion.go b/goextVersion.go index 30075c7..1e262c2 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.169" +const GoextVersion = "0.0.170" -const GoextVersionTimestamp = "2023-07-05T19:27:49+0200" +const GoextVersionTimestamp = "2023-07-17T12:42:49+0200" diff --git a/wmo/queryAggregate.go b/wmo/queryAggregate.go index 00a747a..beb9c7c 100644 --- a/wmo/queryAggregate.go +++ b/wmo/queryAggregate.go @@ -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") +}