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

SingleResult.Decode() not functioning as per Documentation

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 1.0.0
    • Component/s: BSON, Documentation
    • Labels:
      None
    • Environment:
      Windows 10

        //This is the code I have used, it produces no errors however, JSONData.Path is an empty string on completion. This is how to documentation says to do it.
       
      mongoContext, cancelContext := context.WithTimeout(context.Background(), 10*time.Second)

        defer cancelContext()

        mongoClient, _ := mongo.Connect(mongoContext, options.Client().ApplyURI("mongodb://localhost:27017"))
        moviesCollection := mongoClient.Database("Entertainment").Collection("Movies")

        moviesCollection.InsertOne(mongoContext, bson.M{"_id": "Deadpool", "Path": "path/to/file"{color:#bbbbbb}})
        

      singleResult := moviesCollection.FindOne(context.Background(), bson.M{"_id": "Deadpool"{color:#bbbbbb}})

        
      if singleResult.Err() != nil {
          log.Println("Find error: ", singleResult.Err())
        }

        JSONData := struct {
          Path string `json:"Path"`
        }{}

        decodeError := singleResult.Decode(&JSONData)

        if decodeError != nil {
          log.Println("Decode error: ", decodeError)
        }

        fmt.Println("Path: ", JSONData.Path)

            Assignee:
            isabella.siu@mongodb.com Isabella Siu (Inactive)
            Reporter:
            dylan.hoefsloot@outlook.com Dylan Hoefsloot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: