mongo

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2024 License: MIT Imports: 11 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 added in v0.5.0

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

BuildQueries builds the queries without executing them

func NewErrInvalidResults added in v0.6.6

func NewErrInvalidResults(message string) error

func NewErrPaginatedFieldNotFound added in v0.6.6

func NewErrPaginatedFieldNotFound(fieldName string) error

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 added in v0.4.1

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

func (*CursorError) Error added in v0.4.1

func (e *CursorError) Error() string

type ErrInvalidResults added in v0.6.6

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

func (*ErrInvalidResults) Error added in v0.6.6

func (e *ErrInvalidResults) Error() string

type ErrPaginatedFieldNotFound added in v0.6.6

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

func (*ErrPaginatedFieldNotFound) Error added in v0.6.6

func (e *ErrPaginatedFieldNotFound) 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
	// This parameter will also apply timeout of counting total results
	Collation *options.Collation
	// The value to start querying the page
	Next string
	// The value to start querying previous page
	Previous string
	// Whether 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{}
	// This parameter will set the maxTimeMS option on the mongo find cursor, making sure we add a limit to the amount of time
	// mongo can process this on the backend. Will default to 45 seconds, but should be set to an appropriate duration
	// This parameter will also apply timeout of counting total results
	Timeout time.Duration
	// The names of multiple fields being paginated and sorted on. Takes precedence over PaginatedField
	PaginatedFields []string
	// The sort orders corresponding to PaginatedFields. Each value must be either 1 or -1
	SortOrders []int
}

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

type MongoCursor added in v0.3.0

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