mongopagination

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 6 Imported by: 0

README

Golang Mongo Pagination For Package mongo-go-driver

Workflow Build Go Report Card GoDoc Coverage Status

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

🔈 🔈 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() {
	// Establishing mongo db connection
	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")

	//Example for Aggregation

	//match query
	match := bson.M{"$match": bson.M{"qty": bson.M{"$gt": 10}}}

	//group query
	projectQuery := bson.M{"$project": bson.M{"_id": 1, "qty": 1}}

    // set collation if required
    collation := options.Collation{
		Locale:    "en",
		CaseLevel: true,
	}

	// you can easily chain function and pass multiple query like here we are passing match
	// query and projection query as params in Aggregate function you cannot use filter with Aggregate
	// because you can pass filters directly through Aggregate param
	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)
		}

	}

	// print ProductList
	fmt.Printf("Aggregate Product List: %+v\n", aggProductList)

	// print pagination data
	fmt.Printf("Aggregate Pagination Data: %+v\n", aggPaginatedData.Data)
}

For Normal queries



func main() {
	// Establishing mongo db connection
	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
	client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
	if err != nil {
		panic(err)
	}

	// Example for Normal Find query
    	filter := bson.M{}
    	var limit int64 = 10
    	var page int64 = 1
    	collection := client.Database("myaggregate").Collection("stocks")
    	projection := bson.D{
    		{"name", 1},
    		{"qty", 1},
    	}
    	// Querying paginated data
    	// Sort and select are optional
        // Multiple Sort chaining is also allowed
        // If you want to do some complex sort like sort by score(weight) for full text search fields you can do it easily
        // sortValue := bson.M{
        //		"$meta" : "textScore",
        //	}
        // aggPaginatedData, err := paginate.New(collection).Context(ctx).Limit(limit).Page(page).Sort("score", sortValue)...
        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)
    	}
    
    	// paginated data or paginatedData.Data will be nil because data is already decoded on through Decode function
    	// pagination info can be accessed in  paginatedData.Pagination
    	// print ProductList
    	fmt.Printf("Normal Find Data: %+v\n", products)
    
    	// print pagination data
    	fmt.Printf("Normal find pagination info: %+v\n", paginatedData.Pagination)
}
    

Notice:

paginatedData.data //it will be nil incase of  normal queries because data is already decoded on through Decode function

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

  1. https://github.com/mongodb/mongo-go-driver

MIT License

Copyright (c) 2021

Documentation

Index

Constants

View Source
const (
	PageLimitError         = "page or limit cannot be less than 0"
	DecodeEmptyError       = "struct should be provide to decode data"
	DecodeNotAvail         = "this feature is not available for aggregate query"
	FilterInAggregateError = "you cannot use filter in aggregate query but you can pass multiple filter as param in aggregate function"
	NilFilterError         = "filter query cannot be nil"
)

Error constants

Variables

This section is empty.

Functions

func Paging

func Paging(p *pagingQuery, paginationInfo chan<- *Paginator, aggregate bool, aggCount int64)

Paging returns Paginator struct which hold pagination stats

Types

type AutoGenerated

type AutoGenerated struct {
	Total []struct {
		Count int64 `json:"count"`
	} `json:"total"`
	Data []bson.Raw `json:"data"`
}

AutoGenerated is to bind Aggregate query result data

type PaginatedData

type PaginatedData struct {
	Data       []bson.Raw     `json:"data"`
	Pagination PaginationData `json:"pagination"`
}

PaginatedData struct holds data and pagination detail

type PaginationData

type PaginationData struct {
	Total     int64 `json:"total"`
	Page      int64 `json:"page"`
	PerPage   int64 `json:"perPage"`
	Prev      int64 `json:"prev"`
	Next      int64 `json:"next"`
	TotalPage int64 `json:"totalPage"`
}

PaginationData struct for returning pagination stat

type Paginator

type Paginator struct {
	TotalRecord int64 `json:"total_record"`
	TotalPage   int64 `json:"total_page"`
	Offset      int64 `json:"offset"`
	Limit       int64 `json:"limit"`
	Page        int64 `json:"page"`
	PrevPage    int64 `json:"prev_page"`
	NextPage    int64 `json:"next_page"`
}

Paginator struct for holding pagination info

func (*Paginator) PaginationData

func (p *Paginator) PaginationData() *PaginationData

PaginationData returns PaginationData struct which holds information of all stats needed for pagination

type PagingQuery

type PagingQuery interface {
	// Find set the filter for query results.
	Find() (paginatedData *PaginatedData, err error)

	Aggregate(criteria ...interface{}) (paginatedData *PaginatedData, err error)

	// Select used to enable fields which should be retrieved.
	Select(selector interface{}) PagingQuery
	Filter(selector interface{}) PagingQuery
	TotalCount(total int64) PagingQuery
	Limit(limit int64) PagingQuery
	Page(page int64) PagingQuery
	Sort(sortField string, sortValue interface{}) PagingQuery
	Decode(decode interface{}) PagingQuery
	Context(ctx context.Context) PagingQuery
	SetCollation(ctx *options.Collation) PagingQuery
}

PagingQuery is an interface that provides list of function you can perform on pagingQuery

func New

func New(collection *mongo.Collection) PagingQuery

New is to construct PagingQuery object with mongo.Database and collection name

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL