Add Find with offset
This commit is contained in:
12
pipeline.go
12
pipeline.go
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user