Hello everybody,
I write here as I do not find help anywhere.
I have the following Go Code using mongo-go-driver
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,required"`
Name string `bson:"name,required"`
}
type Alias struct {
AA 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{AA: aa, ID: mongoutil.UUID(aa.ID)}
_, err := coll.InsertOne(context.Background(), bb)
fmt.Println(err)
}
where mongoutil
is an helper package which puts UUIDs with correct binary subtype (0x04)
The problem is, since I am creating a mongo adapter, for generic UUID data I need to use composition to replace all uuid.UUID with mongoutil.UUID when I insert a document, and use the composed uuid.UUID after setting it back when I read and return a value.
However this code leads to a duplicate key error
cannot transform type main.Alias to a BSON Document: struct main.Alias) duplicated key id
Anybody can help me in restructuring the A and Alias structs to allow to print a value similar to the following one (image from local mongoDB Compass) ?
As a side note, the following works, but Since in the real scenario I will not have access to the A type from the package in question I would like to avoid using “-” as bson tag
type A struct {
ID uuid.UUID `bson:"-"` // <------------
Name string `bson:"name,required"`
}
type Alias struct {
AA A `bson:",inline"`
ID mongoutil.UUID `bson:"id,required"`
}