func (u *user) Upsert(user *model.User) (*model.User, error) { changed, err := u.C().Upsert(Query{"id": user.ID}, user) if err != nil { return nil, err } if changed.UpsertedId == nil { return u.First(Query{"id": user.ID}), nil } else { user.ObjectId = changed.UpsertedId.(bson.ObjectId) return user, nil } }
func (u *user) Insert(user *model.User) error { user.ObjectId = bson.NewObjectId() return u.C().Insert(user) }