Save Interface in DB golang

36 views Asked by At

I want to store my object into MongoDB in golang. The object 'A' contains a field:

Messages []*Message `bson:"messages,omitempty" json:"messages"`

This array is an array of interface pointers. This is my interface:

type Message interface {

    GetType() enums.MessageType
    GetId() string
}

How can I save this field in mongoDB without creating the messages bson manually? Today I am creating a bson by iterating on all the messages

This is my insert command:

_, err := h.db.Collection(collectionName).InsertOne(ctx, A)

I tried to convert my array to a map[string]*Message and use the bson inline flag.

I tried to Marshall the entire object and got an error that there is no encoder to the interface.

0

There are 0 answers