change user status from capture to activated in mongoDB

74 Views Asked by At

Here, all i am trying to achieve is to change a user status from CAPTURE to ACTIVATED in the mongodb using golang. This is what i have done so far but yet i am not getting the right response.

So far, I have tried the following implementation but nothing happened... All i am expecting is to see a change on an existing user status.


func UpdateUserActivate(client *mongo.Client, user models.User) (*models.User, error) {
    collection := client.Database("coche").Collection("user_entity")
    _id, err := primitive.ObjectIDFromHex(user.ID)
    if err != nil {
        fmt.Println(err)
    }
    
    filter := bson.M{"_id": _id}

    update := bson.M{"$set": bson.M{user.Status: "ACTIVATED"}}
    _, _err := collection.UpdateOne(context.TODO(), filter, update)
    if _err != nil {
        return &user, errors.New("user activation failed")
    }
    return &user, nil
}
This is my user model.




type User struct {
    gorm.Model
    ID          string   `_id`
    FirstName   string   `json:"firstName"bson:"firstname"`
    LastName    string   `json:"lastName" bson: "lastname"`
    Address     string   `json:"address" bson: "address"`
    PhoneNumber string   `json:"phoneNumber" bson: "phonenumber"`
    Status      string   `json:"status" bson: "status"`
    Email       string   `gorm:"unique" json:"email" "email"`
    Password    string   `json:"password""password"`
    Category    string   `json:"category" "category"`
    Roles       []string `json:"roles" "roles"`
}

1

There are 1 best solutions below

2
On

Try update := bson.D{{"$set", bson.D{{"status": "ACTIVATED"}}}}

After "$set" use a comma ","

bson.M or bson.D shouldn't matter in that case. Just take care of the curly brackets using the one or the other.

Ref: https://www.mongodb.com/docs/drivers/go/current/usage-examples/updateOne/