Search code examples
mongodbgomgo

Omit multiple fields with mgo


I have the following structure and I would like to not insert fields Profit and EuroProfit.

type Order struct {
Price      float64    `json:"price"`
EuroPrice  float64    `json:"euro_price"`
Profit     float64    `json:"profit" bson:"_"`
EuroProfit float64    `json:"euro_profit" bson:"_"`
Currency   *Currency  `json:"currency"`
Date       customTime `json:"date"` }

I read in the mgo/bson doc I have to add bson:"_" to avoid to insert them.

But when I insert a structure I get the following error:

Duplicated key '_' in struct model.Order

And indeed, if I let only one ' _ ', it is inserted in the mongodb, so the bson:" _ " seems doesn't work.

How can I avoid to insert them ?

I use the following import:

"github.com/globalsign/mgo/bson"


Solution

  • This looks like a typo. To ignore a field, the tag value must be a hyphen, not an underscore.

    Profit     float64    `json:"profit" bson:"-"`
    EuroProfit float64    `json:"euro_profit" bson:"-"`