[GODRIVER-840] Docs out of date for how to connect Created: 20/Feb/19  Updated: 28/Oct/23  Resolved: 04/Mar/19

Status: Closed
Project: Go Driver
Component/s: Connections, Documentation
Affects Version/s: None
Fix Version/s: 1.0.0-rc2

Type: Bug Priority: Major - P3
Reporter: Peter Weyand Assignee: Isabella Siu (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Docker pulling from github.com/mongodb/mongo-go-driver/mongo (latest)


Issue Links:
Related
is related to GODRIVER-839 GoDoc for mongo.Client does not match... Closed

 Description   

You guys updated how you handle connections:

https://github.com/mongodb/mongo-go-driver/commit/32946b1f8b9412a6a94e68ff789575327bb257cf

but the documentation here:

https://godoc.org/github.com/mongodb/mongo-go-driver/mongo

and here:

https://github.com/mongodb/mongo-go-driver

is out of date.

It now needs to look something like something shown below (tested/compiles). Please update your docs or you will have a lot of confused people in the morning. I'll also update this SO: https://stackoverflow.com/questions/54778520/mongo-go-driver-failing-to-connect. Thanks!

 
package database
import(
  "fmt"
  "context"
  "os"
  "time"
  "github.com/mongodb/mongo-go-driver/mongo"
  "github.com/mongodb/mongo-go-driver/mongo/options"
)
func CreateConnectionHandler()(*mongo.Database, error){
  fmt.Println("inside createConnection in database package")
  godotenv.Load()
  fmt.Println("in CreateConnectionHandler and SERVER_CONFIG: ")
  fmt.Println(os.Getenv("SERVER_CONFIG"))
  uri:=""
  if os.Getenv("SERVER_CONFIG")=="kubernetes"{
    fmt.Println("inside kubernetes db config")
    uri = "mongodb://patientplatypus:SUPERSECRETPASSDOOTb@mongo-release-mongodb.default.svc.cluster.local:27017/platypusNEST?authMechanism=SCRAM-SHA-1"
  }else if os.Getenv("SERVER_CONFIG")=="compose"{
    fmt.Println("inside compose db config")
    uri = "mongodb://datastore:27017"
  }
  client, err := mongo.NewClient(options.Client().ApplyURI(uri))
  if err != nil {
    fmt.Println("error: ", err)
  }
  ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  defer cancel()
  err = client.Connect(ctx)
  if err != nil {
    fmt.Println(err)
  }

  // ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
  // client, err := mongo.Connect(ctx, uri)
  // if err != nil {
  //  return nil, fmt.Errorf("mongo client couldn't connect with background context: %v", err)
  // }
  DB := client.Database("platypusNEST")
  return DB, nil
{color:#c5c8c6}}
 



 Comments   
Comment by Oz Madar [ 20/Feb/19 ]

cursor.Current.Decode also not working (decode is undefined for []byte).

This is a fully working example:

package main
 
// $ go get github.com/mongodb/mongo-go-driver
// -- expect the following response
//package github.com/mongodb/mongo-go-driver: no Go files in /Users/ozma/gopath/src/github.com/mongodb/mongo-go-driver
 
import (
   "context"
   "github.com/mongodb/mongo-go-driver/bson"
   "time"
 
   "fmt"
   "github.com/mongodb/mongo-go-driver/mongo/options"
 
   "github.com/mongodb/mongo-go-driver/mongo"
)
 
type Trainer struct {
   Name string
   Age  int
}
 
func main() {
   var uri = "mongodb://localhost:27017"
   var client *mongo.Client
   var err error
   client, err = mongo.NewClient(options.Client().ApplyURI(uri))
 
   if err != nil {
      fmt.Println("error: ", err)
   }
 
   var ctx context.Context
   var cancel context.CancelFunc
   ctx, cancel = context.WithTimeout(context.Background(), 20*time.Second)
   defer cancel()
   //fmt.Println(client, ctx, cancel)
   err = client.Connect(ctx)
   if err != nil {
      fmt.Println(err)
   }
 
   DB := client.Database("gotest")
 
   var doc = Trainer { Name:"abcde", Age: 44 }
   fmt.Println("--- original doc: ", doc)
 
   col := DB.Collection("col1")
   col.InsertOne(ctx, doc)
 
   filter := bson.D{{"age", doc.Age}}
 
   var res, _ = col.Find(ctx, filter)
   if res.Next(ctx) {
      var resDoc = res.Current
      fmt.Println("--- raw bson from db: ", res.Current)
 
      var trainerRes Trainer
 
      var resParsedErr = bson.Unmarshal(resDoc, &trainerRes)
      if resParsedErr != nil {
         fmt.Println("error: ", resParsedErr)
      } else {
         fmt.Println("--- doc from db: ", trainerRes)
      }
   }
}

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