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

BSON Key conflict in unmarshaling

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.4.4
    • Affects Version/s: None
    • Component/s: BSON
    • None

      Hello, I am trying to do the following

      ``` go
      package main

      import (
          "fmt"

          "github.com/gofrs/uuid"
          "gitlab.tryvium.io/booking-platform/booking-platform-backend/mongoutil"
          "go.mongodb.org/mongo-driver/mongo"
          "go.mongodb.org/mongo-driver/mongo/options"
          "golang.org/x/net/context"
      )

      type A struct 

      {     ID   uuid.UUID `bson:"id"`     Name string    `bson:"name,required"` }

      type Alias struct 

      {     A  `bson:",inline"`     ID mongoutil.UUID `bson:"id,required"` }

      func main() 

      {     c, _ := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://admin:admin@localhost:27017"))     coll := c.Database("tryvium").Collection("test")     id, _ := uuid.NewV4()     aa := A\{ID: id, Name: "test"}

          bb := Alias{A: aa, ID: mongoutil.UUID(aa.ID)}
          _, err := coll.InsertOne(context.Background(), bb)

          fmt.Println(err)
      }
      ```

      However I get the following error on run

      `cannot transform type main.Alias to a BSON Document: struct main.Alias) duplicated key id`

      Is there any way to restructure my 2 structs to achieve what I want, which is having ID field changed in `Alias` struct?

        1. image-2020-10-25-20-36-37-061.png
          image-2020-10-25-20-36-37-061.png
          4 kB
        2. test.go
          0.7 kB

            Assignee:
            isabella.siu@mongodb.com Isabella Siu (Inactive)
            Reporter:
            a.sanino@tryvium.io Alessandro Sanino
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: