[GODRIVER-1260] Collection.find() is not returning all documents Created: 28/Aug/19  Updated: 05/Sep/19  Resolved: 05/Sep/19

Status: Closed
Project: Go Driver
Component/s: CRUD
Affects Version/s: 1.0.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Kshama Jain Assignee: Divjot Arora (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Collection.find() is not returning all documents always. 

For example when I run following query:

```
db.namespaces.find({"clustername":"mylocal.cluster"})
```

It should return all documents (namespaces) (281 documents) for which cluster name is "mylocal.cluster". But, sometimes it only returns 101 documents.



 Comments   
Comment by Divjot Arora (Inactive) [ 05/Sep/19 ]

kshamajain99 Thank you for following up on this quickly. I'm going to go ahead and close the issue.

Comment by Kshama Jain [ 05/Sep/19 ]

Sorry, but I would want to close this issue. We had issue with our grpc context, because of which cursor was closing. Hence, we were receiving only 101 numbers of documents which is a cursor batch size. And, we were not seeing any error which could tell us that cursor is closing without processing all documents. 

Comment by Divjot Arora (Inactive) [ 30/Aug/19 ]

kshamajain99 Can you provide more details or some code to repro this behavior? The following works for me and returns all documents:

package mainimport (
    "context"
    "log"
 
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)
 
func main() {
    ctx := context.Background()
    opts := options.Client().ApplyURI("mongodb://localhost:27017")
    client, err := mongo.Connect(ctx, opts)
    if err != nil {
        log.Fatal(err)
    }
 
    coll := client.Database("foo").Collection("bar")
 
    // clear collection contents
    _, err = coll.DeleteMany(ctx, bson.D{})
    if err != nil {
        log.Fatal(err)
    }
 
    // insert 281 docs
    var docs []interface{}
    for i := 0; i < 281; i++ {
        docs = append(docs, bson.D{{"_id", i}})
    }
    _, err = coll.InsertMany(ctx, docs)
    if err != nil {
        log.Fatal(err)
    }
    
    // verify that 281 docs are found
    cursor, err := coll.Find(ctx, bson.D{})
    if err != nil {
        log.Fatal(err)
    }
    defer func() {
        _ = cursor.Close(ctx)
    }()    cursorCount := 0
    for cursor.Next(ctx) {
        cursorCount++
    }
    if err := cursor.Err(); err != nil {
        log.Fatal(err)
    }
    if cursorCount != 281 {
        log.Fatalf("cursor returned %d", cursorCount)
    }
}

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