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

Update "FindOne" to accept a result output argument, remove SingleResult

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Major Change

      The current FindOne API returns a SingleResult instead of a result/cursor and an error. That confuses users for a number of reasons:

      1. All other operation functions return either a result type and an error or a Cursor and an error. FindOne breaks that pattern and returns a result+error
        "handle" (i.e. the SingleResult) that you then have to make additional calls to, leading to confusion.
      2. SingleResult has an Err() function and Decode also returns an error. That's similar to the Cursor API where users need to check Err() after every call to Next(), but users don't actually need to check the Err() error after calling Decode on a SingleResult, which is confusing.

      Instead, we should change the FindOne API to accept an output parameter and return a single error. To support the DecodeBytes use case, we would add a special behavior if the input type is a *bson.Raw that returns the result BSON document bytes instead of unmarshaling it.

      E.g. FindOne function signature:

      func (c *Collection) FindOne(ctx context.Context, val interface{}, opts ...*options.FindOneOptions) error
      

      E.g. usage:

      coll := client.Database("test").Collection("test")
      
      var myValue struct {
          S string
          I int
      }
      err := coll.FindOne(context.Background(), &myValue)
      if err != nil {
          panic(err)
      }
      

      E.g. special *bson.Raw usage:

      coll := client.Database("test").Collection("test")
      
      var myBytes bson.Raw
      err := coll.FindOne(context.Background(), &myBytes)
      if err != nil {
          panic(err)
      }
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            matt.dale@mongodb.com Matt Dale
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: