[GODRIVER-1831] Find `Limit` Options not work in MongoDB 2.6 Created: 12/Jan/21  Updated: 28/Oct/23  Resolved: 27/Jan/21

Status: Closed
Project: Go Driver
Component/s: API
Affects Version/s: 1.4.4
Fix Version/s: 1.4.6

Type: Bug Priority: Major - P3
Reporter: Lau Liqiang Assignee: Benji Rewis (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDB == 2.6.12
mongo-go-driver 1.4.4


Issue Links:
Related
is related to GODRIVER-1728 Send lower batchSize on getMore comma... Closed

 Description   

First I run a MongoDB 2.6 with docker: 

docker run -p 27018:27017 --name mongo26 -d mongo:2.6

Then I using mongo-go-driver to try List `Limit` Option, the code is following:

package main
 
import (
    "context"
    "go.mongodb.org/mongo-driver/bson"
 
    "github.com/bxcodec/faker"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)
 
type Model struct {
    A int64 `bson:"a,omitempty"`
}
 
func main() {
    opts := options.Client().ApplyURI("mongodb://127.0.0.1:27018")
    client, err := mongo.NewClient(opts)
    if err != nil {
        panic(err)
    }
 
    if err := client.Connect(context.Background()); err != nil {
        panic(err)
    }
 
    genData(client)
    listData(client)
}
 
func genData(cli *mongo.Client) {
    var coll = cli.Database("test").Collection("test")
    for i := 0; i < 1001; i++ {
        m := Model{A: faker.RandomUnixTime()}
        if _, err := coll.InsertOne(context.Background(), m); err != nil {
            panic(err)
        }
    }
}
 
func listData(cli *mongo.Client) {
    var coll = cli.Database("test").Collection("test")
    var data = []Model{}
    var opts = options.Find().SetSkip(0).SetLimit(1000)
    if cursor, err := coll.Find(context.Background(), bson.M{}, opts); err != nil {
        panic(err)
    } else {
        if err = cursor.All(context.Background(), &data); err != nil {
            panic(err)
        }
    }
    println(len(data))
}

Result is 1001,Which we expected to be 1000.



 Comments   
Comment by Kevin Albertson [ 11/Feb/21 ]

Cherry-picked onto release/1.5 with https://github.com/mongodb/mongo-go-driver/commit/f537927df8af103150a7cbf85711a0dbae24d9c9

Comment by Githook User [ 27/Jan/21 ]

Author:

{'name': 'Benjamin Rewis', 'email': '32186188+benjirewis@users.noreply.github.com', 'username': 'benjirewis'}

Message: GODRIVER-1831 Avoid accidental extra getMore when using limit on Find (#562)
Branch: release/1.4
https://github.com/mongodb/mongo-go-driver/commit/3ffdf762d89cee145a24e54feee5225f5def1260

Comment by Benji Rewis (Inactive) [ 27/Jan/21 ]

liqianglau@outlook.com This issue should be fixed in version 1.4.6 (the upcoming stable release) of the driver.

Comment by Githook User [ 27/Jan/21 ]

Author:

{'name': 'Benjamin Rewis', 'email': '32186188+benjirewis@users.noreply.github.com', 'username': 'benjirewis'}

Message: GODRIVER-1831 Avoid accidental extra getMore when using limit on Find (#562)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/f8938997989d4e9dd175c90e3b2d307973617857

Comment by Benji Rewis (Inactive) [ 21/Jan/21 ]

Bug fix and new associated tests in this PR:
https://github.com/mongodb/mongo-go-driver/pull/562

Comment by Kevin Albertson [ 19/Jan/21 ]

Hi Lau, thank you for submitting the pull request! Our CI testing platform, Evergreen, needs to be manually authorized to run against external pull requests. The tests are running now. Members of the team will review the PR.

Comment by Lau Liqiang [ 18/Jan/21 ]

Hi,Kevin Albertson:

After some code review and test we find out maybe the promblem is occured here: https://github.com/mongodb/mongo-go-driver/blob/master/x/mongo/driver/batch_cursor.go#L242-L253

As the code show following:

	// Required for legacy operations which don't support limit.
	numToReturn := bc.batchSize
	if bc.limit != 0 && bc.numReturned+bc.batchSize > bc.limit {
		numToReturn = bc.limit - bc.numReturned
		if numToReturn <= 0 {
			err := bc.Close(ctx)
			if err != nil {
				bc.err = err
			}
			return
		}
	}

 

if we not set the batchSize, and set limit to 1000,if bc.numReturned = 1000 ,the result for numToReturn will be 0, and it would be passed a params to getMore below it。

So the result will more than expected 1000。

We are preparing a patch for it,but WIP:https://github.com/mongodb/mongo-go-driver/pull/561,it seems we have some permission problem for CI。Any suggestions for it?

 

Thanks。

 

Comment by Kevin Albertson [ 12/Jan/21 ]

Thank you for the report liqianglau@outlook.com! I was able to reproduce the issue with the script you provided.

Generated at Thu Feb 08 08:37:13 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.