Skip to content

Pagination with all information like Total, Page, PerPage, Prev, Next, TotalPage for mongodb official package mongo-go-driver

License

Notifications You must be signed in to change notification settings

MaxBreida/mongo-go-pagination

 
 

Repository files navigation

Golang Mongo Pagination For Package mongo-go-driver

Build Go Report Card GoDoc

Simple and easy to use Pagination with information like Total, Page, PerPage, Prev, Next and TotalPage.

Install

$ go get -u -v github.com/roshanr83/mongo-go-pagination

or with dep

$ dep ensure -add github.com/roshanr83/mongo-go-pagination

Demo

    filter := bson.M{}
	var limit int64 = 10
	var page int64 = 1
	paging := PagingQuery{
		collection: db.Collection(DatabaseCollectionName),
		filter: filter,
		limit: limit,
		page: page,
		sortField: "createdAt",
		sortValue: -1,
	}
	paginatedData, err := paging.Find()
	
	// paginated data is in paginatedData.Data
	// pagination info can be accessed in  paginatedData.Pagination
	// if you want to marshal data to your defined struct
	
	var lists []TodoTest
    for _, raw := range paginatedData.Data {
        var todo TodoTest
        if err := bson.Unmarshal(raw, &todo); err == nil {
            lists = append(lists, todo)
        }
    }

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.

MIT License

Copyright (c) 2020

About

Pagination with all information like Total, Page, PerPage, Prev, Next, TotalPage for mongodb official package mongo-go-driver

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%