
For all your simple query to aggregation pipeline this is simple and easy to use Pagination driver with information like Total, Page, PerPage, Prev, Next, TotalPage and your actual mongo result. View examples from here
:speaker: :speaker:
For normal queries new feature have been added to directly pass struct and decode data without manual unmarshalling later. Only normal queries support this feature for now. Sort chaining is also added as new feature
Example api response of Normal Query click here.
Example api response of Aggregate Query click here.
View code used in this example from here
Install
$ go get -u -v github.com/gobeam/mongo-go-pagination
or with dep
$ dep ensure -add github.com/gobeam/mongo-go-pagination
For Aggregation Pipelines Query
package main
import (
"context"
"fmt"
. "github.com/gobeam/mongo-go-pagination"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
type Product struct {
Id primitive.ObjectID `json:"_id" bson:"_id"`
Name string `json:"name" bson:"name"`
Quantity float64 `json:"qty" bson:"qty"`
Price float64 `json:"price" bson:"price"`
}
func main() {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
panic(err)
}
var limit int64 = 10
var page int64 = 1
collection := client.Database("myaggregate").Collection("stocks")
match := bson.M{"$match": bson.M{"qty": bson.M{"$gt": 10}}}
projectQuery := bson.M{"$project": bson.M{"_id": 1, "qty": 1}}
collation := options.Collation{
Locale: "en",
CaseLevel: true,
}
aggPaginatedData, err := New(collection).SetCollation(&collation).Context(ctx).Limit(limit).Page(page).Sort("price", -1).Aggregate(match, projectQuery)
if err != nil {
panic(err)
}
var aggProductList []Product
for _, raw := range aggPaginatedData.Data {
var product *Product
if marshallErr := bson.Unmarshal(raw, &product); marshallErr == nil {
aggProductList = append(aggProductList, *product)
}
}
fmt.Printf("Aggregate Product List: %+v\n", aggProductList)
fmt.Printf("Aggregate Pagination Data: %+v\n", aggPaginatedData.Data)
}
For Normal queries
func main() {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
panic(err)
}
filter := bson.M{}
var limit int64 = 10
var page int64 = 1
collection := client.Database("myaggregate").Collection("stocks")
projection := bson.D{
{"name", 1},
{"qty", 1},
}
var products []Product
paginatedData, err := New(collection).Context(ctx).Limit(limit).Page(page).Sort("price", -1).Select(projection).Filter(filter).Decode(&products).Find()
if err != nil {
panic(err)
}
fmt.Printf("Normal Find Data: %+v\n", products)
fmt.Printf("Normal find pagination info: %+v\n", paginatedData.Pagination)
}
Notice:
paginatedData.data
Running the tests
$ go test
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Acknowledgments
- https://github.com/mongodb/mongo-go-driver
MIT License
Copyright (c) 2021