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

Cannot transform struct typed value if there is a map typed fields in it

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Gone away
    • Icon: Major - P3 Major - P3
    • None
    • 0.0.15
    • BSON
    • None
    • Ubuntu 18.04, Golang v1.11.1

    Description

      As stated above, I have a struct like this
       

      type User struct {
          ID string `json:"id"`
          Username string `json:"username"`
          FirstName string `json:"first_name"`
          LastName string `json:"last_name"`
          FullName string `json:"full_name"`
          Email string `json:"email"`
          Avatar string `json:"avatar"`
          Raw map[string]interface{} `json:"raw"` // Raw data
      }
      

       

       And then i tried to push a variable of this type to my database like this

      var usr User
      // Some works here, populating the usr variable
       
      client, err := mongo.NewClient("mongodb://localhost:27017")
      err = client.Connect(context.TODO())
       
       
      collection := mongo.Client.Database("foo").Collection("bar")
      ctx := context.Background()
      result, err := collection.InsertOne(ctx, usr)
      if err != nil {
          panic(err)
      }
      fmt.Println(result.InsertedID)
       
       
      
      

       However, the program create an error like this 

      cannot transform type structs.User to a *bson.Document
      

      After some trial I found out that if the Raw field is populated it will panic like that, but if I set it to {{nil }}everything  will go back to normal.

      Attachments

        Activity

          People

            kris.brandow@mongodb.com Kristofer Brandow (Inactive)
            vietanisme@gmail.com Viet An Tran
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: