Skip to content

Latest commit

 

History

History
74 lines (51 loc) · 1.83 KB

README.md

File metadata and controls

74 lines (51 loc) · 1.83 KB

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