Files
mongo/pipeline.go
George Suntres 38265c15d1 Initial import
2026-03-29 11:38:57 -04:00

52 lines
1.6 KiB
Go

package mongo
import (
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
)
func BuildPaginationPipeline(skip, limit int64, filter bson.M, sort bson.M) mongo.Pipeline {
return mongo.Pipeline{
// 1. GLOBAL FILTER: Always filter first to use indexes
{{Key: "$match", Value: filter}},
// 2. GLOBAL SORT: Sort here so both 'total' and 'data' facets use the same order
{{Key: "$sort", Value: sort}},
// 3. FACET: Split the pipeline into two parallel paths
{{Key: "$facet", Value: bson.D{
// Path A: Get the total count of documents matching the filter
{Key: "metadata", Value: mongo.Pipeline{
{{Key: "$count", Value: "total"}},
}},
// Path B: Get the specific page of data
{Key: "data", Value: mongo.Pipeline{
{{Key: "$skip", Value: skip}},
{{Key: "$limit", Value: limit}},
}},
}}},
}
}
func BuildPaginationPipelineNext(limit int64, filter bson.M, sort bson.M) mongo.Pipeline {
return mongo.Pipeline{
// 1. GLOBAL FILTER: Always filter first to use indexes
{{Key: "$match", Value: filter}},
// 2. GLOBAL SORT: Sort here so both 'total' and 'data' facets use the same order
{{Key: "$sort", Value: sort}},
// 3. FACET: Split the pipeline into two parallel paths
{{Key: "$facet", Value: bson.D{
// Path A: Get the total count of documents matching the filter
{Key: "metadata", Value: mongo.Pipeline{
{{Key: "$count", Value: "total"}},
}},
// Path B: Get the specific page of data
{Key: "data", Value: mongo.Pipeline{
{{Key: "$limit", Value: limit}},
}},
}}},
}
}