[GODRIVER-1191] Collection.Find() returns empty response Created: 05/Jul/19  Updated: 08/Jul/19  Resolved: 08/Jul/19

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

Type: Bug Priority: Major - P3
Reporter: Matthias Schild Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

windows/amd64, go1.12.5



 Description   

I've tried to fetch a collection with the Find method. After putting an empty document as filter, .Find() returns a cursor which is empty. Also no error occures somewhere.

I followed the example from the README.md in the github repository.

Tried following:

  • Lookup database with MongoDB Compass, `test.user` wasn't empty
  • .Insert works
  • .FindOne returns a document
  • Tried Mongo CLI, returns all documents
  • Tried pymongo, also returns all documents

I made a minimal test code:

package main
 
import (
   "go.mongodb.org/mongo-driver/bson"
   "go.mongodb.org/mongo-driver/mongo"
   "go.mongodb.org/mongo-driver/mongo/options"
   "log"
)
 
// fatalErr is a helper method for errors
func fatalErr(err error) {
   if err != nil {
      log.Fatal(err)
   }
}
 
// Connects to the database and returns a client or calls log.Fatal on error
func connectDb() *mongo.Client {
   opt := options.Client().ApplyURI("mongodb://localhost:27017")
   client, err := mongo.NewClient(opt)
   fatalErr(err)
 
   fatalErr(client.Connect(nil))
   return client
}
 
func main() {
   db := connectDb()
   coll := db.Database("test").Collection("user")
 
   // When I use coll.FindOne(nil, bson.D{}) instead, it finds a document.
 
   // test.user contains multiple records. Find still returns nothing.
   cur, err := coll.Find(nil, bson.D{})
   fatalErr(err)
   defer fatalErr(cur.Close(nil))
 
   for cur.Next(nil) {
      // Never called
      log.Println("->", cur.ID())
   }
 
   fatalErr(cur.Err())
   log.Println("Done.")
}

Can someone reproduce this bug? Or has anyone tipps where I can lookup for mistakes I possible made?

 

Greetings, Matthias Schild



 Comments   
Comment by Matthias Schild [ 07/Jul/19 ]

Bug found, ticket can be closed.

The Find function works fine. The bug was me wrongly understanding the defer function of go.
defer deferres the call fo "fatalErr", but not the call of Close, so the cursor was closed immediately, but the error check of fatalErr was deferred. The closed cursor delivered an empty result (would a cursor already closed error not be better?)

So the bug was sitting in front of the screen.

Comment by Matthias Schild [ 05/Jul/19 ]

Additional information:

  • Fetched driver with `go get go.mongodb.org\mongo-driver\mongo`
  • Date: 15.06.2019
Generated at Thu Feb 08 08:35:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.