diff --git a/goextVersion.go b/goextVersion.go index e9d2a2d..cd6c4d1 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.440" +const GoextVersion = "0.0.441" -const GoextVersionTimestamp = "2024-04-29T16:03:58+0200" +const GoextVersionTimestamp = "2024-04-29T17:19:55+0200" diff --git a/wmo/queryList.go b/wmo/queryList.go index 1d4dedd..e93a7a0 100644 --- a/wmo/queryList.go +++ b/wmo/queryList.go @@ -140,6 +140,42 @@ func (c *Coll[TData]) ListWithCount(ctx context.Context, filter ct.Filter, pageS return data, token, count, nil } +func (c *Coll[TData]) ListAllIDs(ctx context.Context, filter ct.RawFilter) ([]string, error) { + type idObject struct { + ID string `bson:"_id"` + } + + pipelineFilter := mongo.Pipeline{} + + if filter != nil { + pipelineFilter = filter.FilterQuery() + } + + extrModPipelineResolved := mongo.Pipeline{} + for _, ppl := range c.extraModPipeline { + extrModPipelineResolved = langext.ArrConcat(extrModPipelineResolved, ppl(ctx)) + } + + pipelineProjectIDs := mongo.Pipeline{} + pipelineProjectIDs = append(pipelineProjectIDs, bson.D{{Key: "$project", Value: bson.M{"_id": 1}}}) + + pipelineList := langext.ArrConcat(pipelineFilter, extrModPipelineResolved, pipelineProjectIDs) + + cursorList, err := c.coll.Aggregate(ctx, pipelineList) + if err != nil { + return nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipelineList).Str("collection", c.Name()).Build() + } + + var res []idObject + + err = cursorList.All(ctx, res) + if err != nil { + return nil, exerr.Wrap(err, "failed to decode entities").Any("pipeline", pipelineList).Str("collection", c.Name()).Build() + } + + return langext.ArrMap(res, func(v idObject) string { return v.ID }), nil +} + func createPaginationPipeline[TData any](coll *Coll[TData], token ct.CursorToken, fieldPrimary string, sortPrimary ct.SortDirection, fieldSecondary *string, sortSecondary *ct.SortDirection, pageSize *int) ([]bson.D, []bson.D, error) { cond := bson.A{}