Add Find with offset

This commit is contained in:
George Suntres
2026-04-10 21:39:50 -04:00
parent 7a48d336e7
commit cca6601bea
2 changed files with 67 additions and 4 deletions

View File

@@ -6,13 +6,10 @@ import (
)
func BuildPaginationPipeline(skip, limit int64, filter bson.M, sort bson.M) mongo.Pipeline {
return mongo.Pipeline{
pipe := 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
@@ -26,6 +23,13 @@ func BuildPaginationPipeline(skip, limit int64, filter bson.M, sort bson.M) mong
}},
}}},
}
// 2. GLOBAL SORT: Sort here so both 'total' and 'data' facets use the same order
if sort != nil {
pipe = append(pipe, bson.D{{Key: "$sort", Value: sort}})
}
return pipe
}
func BuildPaginationPipelineNext(limit int64, filter bson.M, sort bson.M) mongo.Pipeline {