This commit is contained in:
Mike Schwörer 2023-06-06 21:30:22 +02:00
parent d76d7b5cb9
commit 555096102a
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF

View File

@ -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)