mongo

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package mongocursorpagination eases the computation of pagination information of a find mongo query by augmenting the base query with cursor information and returning a cursor.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildQueries

func BuildQueries(ctx context.Context, p FindParams) (queries []bson.M, sort bson.D, err error)

BuildQueries builds the queries without executing them

Types

type Collection

type Collection interface {
	CountDocuments(context.Context, interface{}, ...*options.CountOptions) (int64, error)
	Find(context.Context, interface{}, ...*options.FindOptions) (MongoCursor, error)
}

type Cursor

type Cursor struct {
	// The URL safe previous page cursor to pass in a Find call to get the previous page.
	// This is set to the empty string if there is no previous page.
	Previous string
	// The URL safe next page cursor to pass in a Find call to get the next page.
	// This is set to the empty string if there is no next page.
	Next string
	// true if there is a previous page, false otherwise
	HasPrevious bool
	// true if there is a next page, false otherwise
	HasNext bool
	// Total count of documents matching filter - only computed if CountTotal is True
	Count int
}

Cursor holds the pagination data about the find mongo query that was performed.

func Find

func Find(ctx context.Context, p FindParams, results interface{}) (Cursor, error)

Find executes a find mongo query by using the provided FindParams, fills the passed in result slice pointer and returns a Cursor.

type CursorError

type CursorError struct {
	// contains filtered or unexported fields
}

func (*CursorError) Error

func (e *CursorError) Error() string

type FindParams

type FindParams struct {
	Collection Collection

	// The find query to augment with pagination
	Query primitive.M
	// The number of results to fetch, should be > 0
	Limit int64
	// true, if the results should be sort ascending, false otherwise
	SortAscending bool
	// The name of the mongo collection field being paginated and sorted on. This field must:
	// 1. Be orderable. We must sort by this value. If duplicate values for paginatedField field
	//    exist, the results will be secondarily ordered by the _id
	// 2. Be indexed. For large collections, this should be indexed for query performance
	// 3. Be immutable. If the value changes between paged queries, it could appear twice
	// 4. Match the bson field name the result struct. e.g.:
	//
	//    PaginatedField would be "name" when paginating employees by name
	//
	//    type Employee struct {
	//        ID          bson.ObjectId `bson:"_id"`
	//        Name        string        `bson:"name"`
	//    }
	//
	PaginatedField string
	Collation      *options.Collation
	// The value to start querying the page
	Next string
	// The value to start querying previous page
	Previous string
	// Whether or not to include total count of documents matching filter in the cursor
	// Specifying true makes an additional query
	CountTotal bool
	// The index to use for the operation. This should either be the index name as a string or the index specification
	// as a document. The default value is nil, which means that no hint will be sent.
	Hint interface{}
	// A document describing which fields will be included in the documents returned by the operation. The default value
	// is nil, which means all fields will be included.
	// Example: bson.D{"_id":0, "name": 1}
	Projection interface{}
}

FindParams holds the parameters to be used in a paginated find mongo query that will return a Cursor.

type MongoCursor

type MongoCursor interface {
	Close(context.Context) error
	Decode(interface{}) error
	ID() int64
	Next(context.Context) bool
	TryNext(context.Context) bool
	Err() error
	All(context.Context, interface{}) error
	RemainingBatchLength() int
}

Jump to

Keyboard shortcuts

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