type Sequence struct {
Id primitive.ObjectID `json:"id" form:"id" bson:"_id"`
Field string `json:"field" form:"field" bson:"field"`
Sequence int64 `json:"sequence" form:"sequence" bson:"sequence" `
}
func (model *Sequence) GetNextSequence(field string) (int64, error) {
opts := options.FindOneAndUpdate()
opts.SetUpsert(true)
opts.SetReturnDocument(options.After)
filter := bson.D{{Key: "field", Value: "Account.Number"}}
update := bson.M{"$inc": bson.M{"sequence": 1}}
err := util.C(model).FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(model)
if err != nil {
return -1, err
}
return model.Sequence, nil
}
The first run func,can get correct result.Continue to run func get result ,but not inc, still the first result. What is the right thing to do?Thanks。