Initial import

This commit is contained in:
George Suntres
2026-03-29 11:38:57 -04:00
commit 38265c15d1
25 changed files with 1763 additions and 0 deletions

52
pipeline.go Normal file
View File

@@ -0,0 +1,52 @@
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}},
}},
}}},
}
}