func (mq *minQuery) All(result interface{}, cursorFields ...string) (cursor string, err error) {
	if mq.cursorErr != nil {
		return "", mq.cursorErr
	}
	cmd := bson.D{
		{Name: "find", Value: mq.coll},
		{Name: "limit", Value: mq.limit},
		{Name: "batchSize", Value: mq.limit},
		{Name: "singleBatch", Value: true},
	}
	if mq.filter != nil {
		cmd = append(cmd, bson.DocElem{Name: "filter", Value: mq.filter})
	}
	if mq.sort != nil {
		cmd = append(cmd, bson.DocElem{Name: "sort", Value: mq.sort})
	}
	if mq.projection != nil {
		cmd = append(cmd, bson.DocElem{Name: "projection", Value: mq.projection})
	}
	if mq.min != nil {
		cmd = append(cmd,
			bson.DocElem{Name: "skip", Value: 1},
			bson.DocElem{Name: "min", Value: mq.min},
		)
		if mq.hint != nil {
			cmd = append(cmd, bson.DocElem{Name: "hint", Value: mq.hint})
		}
	}
	var res struct {
		OK       int `bson:"ok"`
		WaitedMS int `bson:"waitedMS"`
		Cursor   struct {
			ID         interface{} `bson:"id"`
			NS         string      `bson:"ns"`
			FirstBatch []bson.Raw  `bson:"firstBatch"`
		} `bson:"cursor"`
	}
	if err = mq.db.Run(cmd, &res); err != nil {
		return
	}
	firstBatch := res.Cursor.FirstBatch
	if len(firstBatch) > 0 {
		if len(cursorFields) > 0 {
			var doc bson.M
			err = firstBatch[len(firstBatch)-1].Unmarshal(&doc)
			if mq.testError {
				err = errTestValue
			}
			if err != nil {
				return
			}
			cursorData := make(bson.D, len(cursorFields))
			for i, cf := range cursorFields {
				cursorData[i] = bson.DocElem{Name: cf, Value: doc[cf]}
			}
			cursor, err = mq.cursorCodec.CreateCursor(cursorData)
			if err != nil {
				return
			}
		}
	} else {
		cursor = mq.cursor
	}
	err = mq.db.C(mq.coll).NewIter(nil, firstBatch, 0, nil).All(result)
	return
}
All implements MinQuery.All().
package minquery
import (
	"errors"
	"github.com/globalsign/mgo"
	"github.com/globalsign/mgo/bson"
)
var DefaultCursorCodec cursorCodec
type MinQuery interface {
	Sort(fields ...string) MinQuery
	Select(selector interface{}) MinQuery
	Limit(n int) MinQuery
	Cursor(c string) MinQuery
	CursorCodec(cc CursorCodec) MinQuery
	All(result interface{}, cursorFields ...string) (cursor string, err error)
}
var errTestValue = errors.New("Intentional testing error")
type minQuery struct {
	db   *mgo.Database
	hint map[string]int
	coll string
	filter interface{}
	sort bson.D
	projection interface{}
	limit int
	cursor string
	cursorCodec CursorCodec
	cursorErr error
	min bson.D
	testError bool
}
func New(db *mgo.Database, coll string, query interface{}) MinQuery {
	return NewWithHint(db, coll, query, nil)
}
func NewWithHint(db *mgo.Database, coll string, query interface{}, hint map[string]int) MinQuery {
	return &minQuery{
		db:          db,
		coll:        coll,
		filter:      query,
		hint:        hint,
		cursorCodec: DefaultCursorCodec,
	}
}
func (mq *minQuery) Sort(fields ...string) MinQuery {
	mq.sort = make(bson.D, 0, len(fields))
	for _, field := range fields {
		if field == "" {
			continue
		}
		n := 1
		if field[0] == '+' {
			field = field[1:]
		} else if field[0] == '-' {
			n, field = -1, field[1:]
		}
		mq.sort = append(mq.sort, bson.DocElem{Name: field, Value: n})
	}
	return mq
}
func (mq *minQuery) Select(selector interface{}) MinQuery {
	mq.projection = selector
	return mq
}
func (mq *minQuery) Limit(n int) MinQuery {
	mq.limit = n
	return mq
}
func (mq *minQuery) Cursor(c string) MinQuery {
	mq.cursor = c
	if c != "" {
		mq.min, mq.cursorErr = mq.cursorCodec.ParseCursor(c)
	} else {
		mq.min, mq.cursorErr = nil, nil
	}
	return mq
}
func (mq *minQuery) CursorCodec(cc CursorCodec) MinQuery {
	mq.cursorCodec = cc
	return mq
}