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

FindOne() nondeterministically returns empty singleResult

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 0.3.0
    • Component/s: BSON, Core API, Error Handling
    • None
    • Environment:
      running mongo:3.6-stretch in docker
      go version go1.11.5 darwin/amd64
      mongo driver github.com/mongodb/mongo-go-driver v0.3.0

      When expecting a single result, and calling FindOne(), an empty SingleResult is sometimes* returned instead - which Decode() handles by returning ErrNoDocuments. 

      *sometimes seems to be approx. 10% of the time

      Find below an example, with steps to reproduce:

      Setup collection & insertOne

      db.mycollection.insertOne({"name":"some-name", "address": {"first_line":"expected_first_line","second_line":"expected_second_line"}, "age": 10 } )

      Structs in code:

       

      type Address struct {
         FirstLine  string `bson:"first_line"`
         SecondLine string `bson:"second_line"`
      }
      
      type User struct {
         ID      primitive.ObjectID `bson:"_id,omitempty"`
         Name    string             `bson:"name"`
         Address Address            `bson:"address"`
         Age     int                `bson:"age"`
      }
      

       

       

      Call findOne(), and Decode() in code:

       

      func main() {
         mongoClient, err := mongo.NewClient(mongoURI)
         if err != nil {
            return
         }
      
         err = mongoClient.Connect(context.Background())
         if err != nil {
            return
         }
      
      
         _, err = GetUser(mongoClient)
         if err != nil {
            if err == mongo.ErrNoDocuments {
               fmt.Println(err)
            }
         }
      }
      
      func GetUser(mongoClient *mongo.Client) (*User, error) {
         var actualUser User
      
         singleResult := mongoClient.
            Database(mongoDB).
            Collection(mongoCollection).
            FindOne(
               context.Background(),
               bson.M{
                  "address": bson.M{
                     "first_line":  expectedFirstLine,
                     "second_line": expectedSecondLine},
               })
      
         if singleResult.Err() != nil {
            return nil, singleResult.Err()
         }
      
         err := singleResult.Decode(&actualUser)
         return &actualUser, err
      }
      

       

      As stated previously, in most cases, this will successfully decode the document into the actualUser struct; however sometimes the decode will instead return an ErrNoDocument.

       

       

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            matthewnolf Matthew Nolf
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: