0
votes

I have this common struct for all other structs.

// Base contains common fields for all documents as given below.

type Base struct {
    CreatedAt time.Time `json:"createdAt"  bson:"created_at"`
    UpdatedAt time.Time `json:"updatedAt" bson:"updated_at"`
    DeletedAt time.Time `json:"deletedAt,omitempty" bson:"deleted_at"`
}

type Nice struct {
    Base
    Notes             string             `json:"notes" bson:"notes"`
}

Now issue is go-Mongo save it as nested object with name base as follows and I want to avoid it to save it as nested object. What is the way to avoid it, I am unable to find anything in documentation

{ "_id" : ObjectId("6154807677b7f58b6438b71c"), "base" : { "created_at" : ISODate("2021-09-29T15:04:22.322Z"), "updated_at" : ISODate("0001-01-01T00:00:00Z"), "deleted_at" : ISODate("0001-01-01T00:00:00Z") } "notes" : ""}
1

1 Answers

0
votes

It is in bson documentation:

inline: If the inline struct tag is specified for a struct or map field, the field will be "flattened" when marshalling and "un-flattened" when unmarshalling.

So use:

type Nice struct {
    Base `bson:",inline"`
    Notes             string             `json:"notes" bson:"notes"`
}