{"id":13563396,"url":"https://github.com/gobeam/mongo-go-pagination","last_synced_at":"2025-04-06T05:18:24.771Z","repository":{"id":39653375,"uuid":"238156866","full_name":"gobeam/mongo-go-pagination","owner":"gobeam","description":"Golang Mongodb Pagination for official mongodb/mongo-go-driver package which supports both normal queries and Aggregation pipelines with all information like Total records, Page, Per Page, Previous, Next, Total Page and query results.","archived":false,"fork":false,"pushed_at":"2023-03-07T01:29:19.000Z","size":102,"stargazers_count":131,"open_issues_count":4,"forks_count":35,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-30T04:09:23.001Z","etag":null,"topics":["aggregate","aggregation","database","driver","go","go-mongo","golang","match","mongo","mongo-go-driver","mongo-go-pagination","mongo-pagination","mongodb","mongodb-driver","pagination","pagination-library","paginator","perpage","pipelines","totalpage"],"latest_commit_sha":null,"homepage":"https://mongo-go-pagination.herokuapp.com/normal-pagination?page=1\u0026limit=15","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gobeam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-04T08:23:33.000Z","updated_at":"2025-02-05T01:17:43.000Z","dependencies_parsed_at":"2024-06-18T13:48:37.140Z","dependency_job_id":"22dd5a5d-c988-4c16-ade0-cddba6561e57","html_url":"https://github.com/gobeam/mongo-go-pagination","commit_stats":null,"previous_names":["roshanr83/mongo-go-pagination"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobeam%2Fmongo-go-pagination","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobeam%2Fmongo-go-pagination/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobeam%2Fmongo-go-pagination/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobeam%2Fmongo-go-pagination/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gobeam","download_url":"https://codeload.github.com/gobeam/mongo-go-pagination/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247436580,"owners_count":20938592,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aggregate","aggregation","database","driver","go","go-mongo","golang","match","mongo","mongo-go-driver","mongo-go-pagination","mongo-pagination","mongodb","mongodb-driver","pagination","pagination-library","paginator","perpage","pipelines","totalpage"],"created_at":"2024-08-01T13:01:18.744Z","updated_at":"2025-04-06T05:18:24.750Z","avatar_url":"https://github.com/gobeam.png","language":"Go","funding_links":[],"categories":["工具库","Utilities","公用事业公司","Go","工具库`可以提升效率的通用代码库和工具`","Utility"],"sub_categories":["查询语","Utility/Miscellaneous","实用程序/Miscellaneous","HTTP Clients","Fail injection"],"readme":"# Golang Mongo Pagination For Package mongo-go-driver\n![Workflow](https://github.com/gobeam/mongo-go-pagination/actions/workflows/ci.yml/badge.svg) [![Build][Build-Status-Image]][Build-Status-Url] [![Go Report Card](https://goreportcard.com/badge/github.com/gobeam/mongo-go-pagination?branch=master\u0026kill_cache=1)](https://goreportcard.com/report/github.com/gobeam/mongo-go-pagination) [![GoDoc][godoc-image]][godoc-url]\n[![Coverage Status](https://coveralls.io/repos/github/gobeam/mongo-go-pagination/badge.png?branch=master)](https://coveralls.io/github/gobeam/mongo-go-pagination?branch=master)\n\nFor 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](https://github.com/gobeam/mongo-go-pagination/tree/master/example)\n\n:speaker: :speaker: \n***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***\n\nExample api response of Normal Query [click here](https://mongo-go-pagination.herokuapp.com/normal-pagination?page=1\u0026limit=10).\u003cbr\u003e\nExample api response of Aggregate Query [click here](https://mongo-go-pagination.herokuapp.com/aggregate-pagination?page=1\u0026limit=10).\u003cbr\u003e\nView code used in this example from [here](https://github.com/gobeam/mongo-go-pagination/tree/master/example)\n\n## Install\n\n``` bash\n$ go get -u -v github.com/gobeam/mongo-go-pagination\n```\n\nor with dep\n\n``` bash\n$ dep ensure -add github.com/gobeam/mongo-go-pagination\n```\n\n\n## For Aggregation Pipelines Query\n\n``` go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t. \"github.com/gobeam/mongo-go-pagination\"\n\t\"go.mongodb.org/mongo-driver/bson\"\n\t\"go.mongodb.org/mongo-driver/bson/primitive\"\n\t\"go.mongodb.org/mongo-driver/mongo\"\n\t\"go.mongodb.org/mongo-driver/mongo/options\"\n\t\"time\"\n)\n\ntype Product struct {\n\tId       primitive.ObjectID `json:\"_id\" bson:\"_id\"`\n\tName     string             `json:\"name\" bson:\"name\"`\n\tQuantity float64            `json:\"qty\" bson:\"qty\"`\n\tPrice    float64            `json:\"price\" bson:\"price\"`\n}\n\nfunc main() {\n\t// Establishing mongo db connection\n\tctx, _ := context.WithTimeout(context.Background(), 10*time.Second)\n\tclient, err := mongo.Connect(ctx, options.Client().ApplyURI(\"mongodb://localhost:27017\"))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\t\n\tvar limit int64 = 10\n\tvar page int64 = 1\n\tcollection := client.Database(\"myaggregate\").Collection(\"stocks\")\n\n\t//Example for Aggregation\n\n\t//match query\n\tmatch := bson.M{\"$match\": bson.M{\"qty\": bson.M{\"$gt\": 10}}}\n\n\t//group query\n\tprojectQuery := bson.M{\"$project\": bson.M{\"_id\": 1, \"qty\": 1}}\n\n    // set collation if required\n    collation := options.Collation{\n\t\tLocale:    \"en\",\n\t\tCaseLevel: true,\n\t}\n\n\t// you can easily chain function and pass multiple query like here we are passing match\n\t// query and projection query as params in Aggregate function you cannot use filter with Aggregate\n\t// because you can pass filters directly through Aggregate param\n\taggPaginatedData, err := New(collection).SetCollation(\u0026collation).Context(ctx).Limit(limit).Page(page).Sort(\"price\", -1).Aggregate(match, projectQuery)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tvar aggProductList []Product\n\tfor _, raw := range aggPaginatedData.Data {\n\t\tvar product *Product\n\t\tif marshallErr := bson.Unmarshal(raw, \u0026product); marshallErr == nil {\n\t\t\taggProductList = append(aggProductList, *product)\n\t\t}\n\n\t}\n\n\t// print ProductList\n\tfmt.Printf(\"Aggregate Product List: %+v\\n\", aggProductList)\n\n\t// print pagination data\n\tfmt.Printf(\"Aggregate Pagination Data: %+v\\n\", aggPaginatedData.Data)\n}\n\n```\n\n## For Normal queries\n``` go\n\n\nfunc main() {\n\t// Establishing mongo db connection\n\tctx, _ := context.WithTimeout(context.Background(), 10*time.Second)\n\tclient, err := mongo.Connect(ctx, options.Client().ApplyURI(\"mongodb://localhost:27017\"))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Example for Normal Find query\n    \tfilter := bson.M{}\n    \tvar limit int64 = 10\n    \tvar page int64 = 1\n    \tcollection := client.Database(\"myaggregate\").Collection(\"stocks\")\n    \tprojection := bson.D{\n    \t\t{\"name\", 1},\n    \t\t{\"qty\", 1},\n    \t}\n    \t// Querying paginated data\n    \t// Sort and select are optional\n        // Multiple Sort chaining is also allowed\n        // If you want to do some complex sort like sort by score(weight) for full text search fields you can do it easily\n        // sortValue := bson.M{\n        //\t\t\"$meta\" : \"textScore\",\n        //\t}\n        // aggPaginatedData, err := paginate.New(collection).Context(ctx).Limit(limit).Page(page).Sort(\"score\", sortValue)...\n        var products []Product\n    \tpaginatedData, err := New(collection).Context(ctx).Limit(limit).Page(page).Sort(\"price\", -1).Select(projection).Filter(filter).Decode(\u0026products).Find()\n    \tif err != nil {\n    \t\tpanic(err)\n    \t}\n    \n    \t// paginated data or paginatedData.Data will be nil because data is already decoded on through Decode function\n    \t// pagination info can be accessed in  paginatedData.Pagination\n    \t// print ProductList\n    \tfmt.Printf(\"Normal Find Data: %+v\\n\", products)\n    \n    \t// print pagination data\n    \tfmt.Printf(\"Normal find pagination info: %+v\\n\", paginatedData.Pagination)\n}\n    \n```\n***Notice:***\n```go\npaginatedData.data //it will be nil incase of  normal queries because data is already decoded on through Decode function\n```\n\n## Running the tests\n\n``` bash\n$ go test\n```\n\n## Contributing\nPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.\n\nPlease make sure to update tests as appropriate.\n\n\n## Acknowledgments\n\u003col\u003e\n\u003cli\u003e https://github.com/mongodb/mongo-go-driver \u003c/li\u003e\n\u003c/ol\u003e\n\n\n## MIT License\n\n```\nCopyright (c) 2021\n```\n\n[Build-Status-Url]: https://travis-ci.com/gobeam/mongo-go-pagination\n[Build-Status-Image]: https://travis-ci.com/gobeam/mongo-go-pagination.svg?branch=master\n[godoc-url]: https://pkg.go.dev/github.com/gobeam/mongo-go-pagination?tab=doc\n[godoc-image]: https://godoc.org/github.com/gobeam/mongo-go-pagination?status.svg\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgobeam%2Fmongo-go-pagination","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgobeam%2Fmongo-go-pagination","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgobeam%2Fmongo-go-pagination/lists"}