[GODRIVER-850] SingleResult.Decode() not functioning as per Documentation Created: 25/Feb/19  Updated: 11/Sep/19  Resolved: 21/Mar/19

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

Type: Task Priority: Minor - P4
Reporter: Dylan Hoefsloot Assignee: Isabella Siu (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10



 Description   

  //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)



 Comments   
Comment by Isabella Siu (Inactive) [ 05/Mar/19 ]

Hi dylan.hoefsloot@outlook.com,

If you add a bson tag to the Path variable in your JSONData struct it should correctly decode your document. That would look like this:

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

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