Initial import
This commit is contained in:
52
pipeline.go
Normal file
52
pipeline.go
Normal 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}},
|
||||
}},
|
||||
}}},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user