From 555096102a502f5c3be0c4d3ce10706f810c2f61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Tue, 6 Jun 2023 21:30:22 +0200 Subject: [PATCH] v0.0.137 --- wmo/collection.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/wmo/collection.go b/wmo/collection.go index 5f44943..6167edd 100644 --- a/wmo/collection.go +++ b/wmo/collection.go @@ -220,6 +220,32 @@ func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int, return entities, nextToken, nil } +func (c *Coll[TData]) Count(ctx context.Context, filter ct.Filter) (int64, error) { + pipeline := filter.FilterQuery() + + pipeline = append(pipeline, bson.D{{Key: "$count", Value: "c"}}) + + cursor, err := c.coll.Aggregate(ctx, pipeline) + if err != nil { + return 0, err + } + + type res struct { + Count int64 `bson:"c"` + } + + if cursor.Next(ctx) { + v := res{} + err = cursor.Decode(&v) + if err != nil { + return 0, err + } + return v.Count, nil + } + + return 0, nil +} + func (c *Coll[TData]) createToken(fieldPrimary string, dirPrimary ct.SortDirection, fieldSecondary *string, dirSecondary *ct.SortDirection, lastEntity TData, pageSize *int) (ct.CursorToken, error) { valuePrimary, err := c.getFieldValueAsTokenString(lastEntity, fieldPrimary)