Skip to content

Commit ab49369

Browse files
authored
Uptoofficial (#237)
1 parent 4ac6b65 commit ab49369

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ go 1.16
55
require (
66
github.com/go-playground/validator/v10 v10.4.1
77
github.com/stretchr/testify v1.6.1
8-
go.mongodb.org/mongo-driver v1.8.2
8+
go.mongodb.org/mongo-driver v1.9.0
99
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyh
4444
github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
4545
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
4646
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
47-
go.mongodb.org/mongo-driver v1.8.2 h1:8ssUXufb90ujcIvR6MyE1SchaNj0SFxsakiZgxIyrMk=
48-
go.mongodb.org/mongo-driver v1.8.2/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY=
47+
go.mongodb.org/mongo-driver v1.9.0 h1:f3aLGJvQmBl8d9S40IL+jEyBC6hfLPbJjv9t5hEM9ck=
48+
go.mongodb.org/mongo-driver v1.9.0/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY=
4949
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
5050
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
5151
golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f h1:aZp0e2vLN4MToVqnjNEYEtrEA8RH8U8FN1CU7JgqsPU=

query.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,18 @@ type Query struct {
4343
registry *bsoncodec.Registry
4444
}
4545

46-
// Sort is Used to set the sorting rules for the returned results
47-
// Format: "age" or "+age" means to sort the age field in ascending order, "-age" means in descending order
48-
// When multiple sort fields are passed in at the same time, they are arranged in the order in which the fields are passed in.
49-
// For example, {"age", "-name"}, first sort by age in ascending order, then sort by name in descending order
50-
46+
// BatchSize sets the value for the BatchSize field.
47+
// Means the maximum number of documents to be included in each batch returned by the server.
5148
func (q *Query) BatchSize(n int64) QueryI {
5249
newQ := q
5350
newQ.batchSize = &n
5451
return newQ
5552
}
5653

54+
// Sort is Used to set the sorting rules for the returned results
55+
// Format: "age" or "+age" means to sort the age field in ascending order, "-age" means in descending order
56+
// When multiple sort fields are passed in at the same time, they are arranged in the order in which the fields are passed in.
57+
// For example, {"age", "-name"}, first sort by age in ascending order, then sort by name in descending order
5758
func (q *Query) Sort(fields ...string) QueryI {
5859
if len(fields) == 0 {
5960
// A nil bson.D will not correctly serialize, but this case is no-op
@@ -155,7 +156,6 @@ func (q *Query) All(result interface{}) error {
155156
}
156157
}
157158
opt := options.Find()
158-
159159
if q.sort != nil {
160160
opt.SetSort(q.sort)
161161
}
@@ -171,7 +171,6 @@ func (q *Query) All(result interface{}) error {
171171
if q.hint != nil {
172172
opt.SetHint(q.hint)
173173
}
174-
175174
if q.batchSize != nil {
176175
opt.SetBatchSize(int32(*q.batchSize))
177176
}

query_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,3 +830,28 @@ func TestQuery_Apply(t *testing.T) {
830830
ast.Equal("", res4.Name)
831831
ast.Equal(0, res4.Age)
832832
}
833+
834+
func TestQuery_BatchSize(t *testing.T) {
835+
ast := require.New(t)
836+
cli := initClient("test")
837+
defer cli.Close(context.Background())
838+
defer cli.DropCollection(context.Background())
839+
cli.EnsureIndexes(context.Background(), nil, []string{"name"})
840+
841+
id1 := primitive.NewObjectID()
842+
id2 := primitive.NewObjectID()
843+
id3 := primitive.NewObjectID()
844+
id4 := primitive.NewObjectID()
845+
docs := []interface{}{
846+
bson.M{"_id": id1, "name": "Alice", "age": 18},
847+
bson.M{"_id": id2, "name": "Alice", "age": 19},
848+
bson.M{"_id": id3, "name": "Lucas", "age": 20},
849+
bson.M{"_id": id4, "name": "Lucas", "age": 21},
850+
}
851+
_, _ = cli.InsertMany(context.Background(), docs)
852+
var res []QueryTestItem
853+
854+
err := cli.Find(context.Background(), bson.M{"name": "Alice"}).BatchSize(1).All(&res)
855+
ast.NoError(err)
856+
ast.Len(res, 2)
857+
}

0 commit comments

Comments
 (0)