Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 9fe1bd1

Browse files
committed
Server closes cursor when batch is 1. Use 2 instead.
1 parent d116e06 commit 9fe1bd1

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

session.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,10 @@ func (s *Session) SetSyncTimeout(d time.Duration) {
951951
// writing, MongoDB will use an initial size of min(100 docs, 4MB) on the
952952
// first batch, and 4MB on remaining ones.
953953
func (s *Session) SetBatch(n int) {
954+
if n == 1 {
955+
// Server interprets 1 as -1 and closes the cursor (!?)
956+
n = 2
957+
}
954958
s.m.Lock()
955959
s.queryConfig.op.limit = int32(n)
956960
s.m.Unlock()
@@ -1591,6 +1595,10 @@ func (c *Collection) Create(info *CollectionInfo) error {
15911595
// writing, MongoDB will use an initial size of min(100 docs, 4MB) on the
15921596
// first batch, and 4MB on remaining ones.
15931597
func (q *Query) Batch(n int) *Query {
1598+
if n == 1 {
1599+
// Server interprets 1 as -1 and closes the cursor (!?)
1600+
n = 2
1601+
}
15941602
q.m.Lock()
15951603
q.op.limit = int32(n)
15961604
q.m.Unlock()

session_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,8 @@ func (s *S) TestQueryExplain(c *C) {
927927
}
928928

929929
m := M{}
930-
query := coll.Find(nil).Batch(1).Limit(2)
931-
err = query.Batch(2).Explain(m)
930+
query := coll.Find(nil).Limit(2)
931+
err = query.Explain(m)
932932
c.Assert(err, IsNil)
933933
c.Assert(m["cursor"], Equals, "BasicCursor")
934934
c.Assert(m["nscanned"], Equals, 2)
@@ -2841,3 +2841,26 @@ func (s *S) TestPipeOne(c *C) {
28412841
err = pipe.One(&result)
28422842
c.Assert(err, Equals, mgo.ErrNotFound)
28432843
}
2844+
2845+
func (s *S) TestBatch1(c *C) {
2846+
session, err := mgo.Dial("localhost:40001")
2847+
c.Assert(err, IsNil)
2848+
defer session.Close()
2849+
2850+
coll := session.DB("mydb").C("mycoll")
2851+
2852+
for i := 0; i < 3; i++ {
2853+
err := coll.Insert(M{"n": i})
2854+
c.Assert(err, IsNil)
2855+
}
2856+
2857+
var ns []struct{ N int }
2858+
err = coll.Find(nil).Batch(1).All(&ns)
2859+
c.Assert(err, IsNil)
2860+
c.Assert(len(ns), Equals, 3)
2861+
2862+
session.SetBatch(1)
2863+
err = coll.Find(nil).All(&ns)
2864+
c.Assert(err, IsNil)
2865+
c.Assert(len(ns), Equals, 3)
2866+
}

0 commit comments

Comments
 (0)