Skip to content

Commit

Permalink
Merge pull request #236 from xsean2020/feature_add_SetbatchSize
Browse files Browse the repository at this point in the history
add set batchSize
  • Loading branch information
jiangz222 authored Apr 9, 2022
2 parents 64e8669 + 93226fa commit 4ac6b65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type QueryI interface {
Sort(fields ...string) QueryI
Select(selector interface{}) QueryI
Skip(n int64) QueryI
BatchSize(n int64) QueryI
Limit(n int64) QueryI
One(result interface{}) error
All(result interface{}) error
Expand Down
28 changes: 22 additions & 6 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ import (

// Query struct definition
type Query struct {
filter interface{}
sort interface{}
project interface{}
hint interface{}
limit *int64
skip *int64
filter interface{}
sort interface{}
project interface{}
hint interface{}
limit *int64
skip *int64
batchSize *int64

ctx context.Context
collection *mongo.Collection
Expand All @@ -46,6 +47,13 @@ type Query struct {
// Format: "age" or "+age" means to sort the age field in ascending order, "-age" means in descending order
// When multiple sort fields are passed in at the same time, they are arranged in the order in which the fields are passed in.
// For example, {"age", "-name"}, first sort by age in ascending order, then sort by name in descending order

func (q *Query) BatchSize(n int64) QueryI {
newQ := q
newQ.batchSize = &n
return newQ
}

func (q *Query) Sort(fields ...string) QueryI {
if len(fields) == 0 {
// A nil bson.D will not correctly serialize, but this case is no-op
Expand Down Expand Up @@ -164,6 +172,10 @@ func (q *Query) All(result interface{}) error {
opt.SetHint(q.hint)
}

if q.batchSize != nil {
opt.SetBatchSize(int32(*q.batchSize))
}

var err error
var cursor *mongo.Cursor

Expand Down Expand Up @@ -259,6 +271,10 @@ func (q *Query) Cursor() CursorI {
opt.SetSkip(*q.skip)
}

if q.batchSize != nil {
opt.SetBatchSize(int32(*q.batchSize))
}

var err error
var cur *mongo.Cursor
cur, err = q.collection.Find(q.ctx, q.filter, opt)
Expand Down

0 comments on commit 4ac6b65

Please sign in to comment.