mongopaging

package module
v0.0.0-...-4d2c5ce Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: MIT Imports: 5 Imported by: 0

README

mongopaging

Mongo paging library using mongo-go-driver.

Demo
  pagingQuery := New(db, "users")
  pagingQuery.Find(bson.M{"email": bson.M{"$ne": ""}}).Limit(10).Sort("-created_at")
  results, cur, err := pagingQuery.Decode(context.Background())
  
  var users []UserModelResp
  for _, raw := range results {
	var user UserModelResp
	bson.Unmarshal(raw, &user)
	users = append(users, user)
  }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cursor

type Cursor interface {
	Create(cursorData bson.D) (string, error)
	Parse(c string) (cursorData bson.D, err error)
}

type PagingQuery

type PagingQuery interface {
	// Find set the filter for query results.
	Find(criteria interface{}) PagingQuery

	// Sort used to do sorting for query results according to sort field.
	// The sort field may contain two parts,
	// prefix {{-}} or {{+}}, represents ascending or descending order and
	// fieldname {{document field name}} which need to be indexed.
	// Default: -_id
	Sort(field string) PagingQuery

	// Limit is to set the maximun number of documents to be retrieved.
	// There is not default limit.
	Limit(count uint) PagingQuery

	// Select used to enable fields which should be retrieved.
	Select(selector interface{}) PagingQuery

	// Cursor is used to do pagination for document query.
	// Documents can be retrieved from that cursor value.
	Cursor(lastCursorValue string) PagingQuery

	// Decode will run the command to database and return result as []bson.Raw and error
	Decode(ctx context.Context) (result []bson.Raw, cursor string, err error)

	// Explain is to print out prepared query for command.
	Explain() string
}

PagingQuery is to construct mongo find command (https://docs.mongodb.com/manual/reference/command/find/#dbcmd.find). And, it will return cursor id (value of sorting field from last document) and result as []bson.Raw

func New

func New(db *mongo.Database, collection string) PagingQuery

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

Jump to

Keyboard shortcuts

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