Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-1831

Find `Limit` Options not work in MongoDB 2.6

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.4.6
    • Affects Version/s: 1.4.4
    • Component/s: API
    • None
    • Environment:
      MongoDB == 2.6.12
      mongo-go-driver 1.4.4

      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.

            Assignee:
            benji.rewis@mongodb.com Benji Rewis (Inactive)
            Reporter:
            liqianglau@outlook.com Lau Liqiang
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: