goext/wmo/queryFind.go

30 lines
789 B
Go
Raw Normal View History

2023-06-06 21:33:49 +02:00
package wmo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
2023-08-21 15:08:35 +02:00
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
2023-06-06 21:33:49 +02:00
)
func (c *Coll[TData]) Find(ctx context.Context, filter bson.M) ([]TData, error) {
2023-06-06 21:33:49 +02:00
pipeline := mongo.Pipeline{}
pipeline = append(pipeline, bson.D{{Key: "$match", Value: filter}})
2023-06-06 21:33:49 +02:00
pipeline = langext.ArrConcat(pipeline, c.extraModPipeline)
2023-06-06 21:33:49 +02:00
cursor, err := c.coll.Aggregate(ctx, pipeline)
2023-06-06 21:33:49 +02:00
if err != nil {
return nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipeline).Str("collection", c.Name()).Build()
2023-06-06 21:33:49 +02:00
}
2023-06-10 16:22:14 +02:00
res, err := c.decodeAll(ctx, cursor)
2023-06-06 21:33:49 +02:00
if err != nil {
2023-08-21 15:08:35 +02:00
return nil, exerr.Wrap(err, "failed to decode values").Build()
2023-06-06 21:33:49 +02:00
}
return res, nil
}