0

I have been having some trouble updating a user on a mongodatabase. Basically I want to select the user by username and than edit its details. I am using Gorilla Mux and mgo to connect with MongoDB.

Here is the code:

func ViewUserHandler(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    username := vars["username"]
    session, err := mgo.Dial("mongodb://DATABASE_URL")

    if err != nil {
        panic(err)
    }

    defer session.Close()
    session.SetMode(mgo.Monotonic, true)
    c := session.DB("studnet").C("user")

    result := Person{}
    // get the user_id using a hidden field when clicked using javascript
    err = c.Find(bson.M{"name": username}).One(&result)
    if err != nil {
        log.Fatal(err)
    }

    if r.Method == "GET" {
        t, _ := template.ParseFiles("profile.html")
        t.Execute(w, result)
    }
    // update the user profile details
    if r.Method == "POST" {
        r.ParseForm()

        // TODO : update the user
        selectedUser := bson.M{"name": username}
        updatedUser := bson.M{"$set": bson.M{
            "Name":      r.Form["username"][0],
            "Gender":    r.Form["gender"][0],
            "Age":       r.Form["age"][0],
            "CreatedAt": time.Now(),
        }}
        err = c.Update(selectedUser, updatedUser)

        if err != nil {
            panic(err)
        }
        http.Redirect(w, r, "/view/"+username, 301)
    }
}
Anonymous Penguin
  • 2,027
  • 4
  • 34
  • 49
  • What happens with your code? Do you get any errors? – TheHippo Dec 21 '15 at 14:37
  • 1
    What was the **question** again? – Markus W Mahlberg Dec 21 '15 at 21:38
  • How can I query a user by username in mgo and than update its details ? Basically i dont know how to use Update function in mgo... Thanks for your time. –  Dec 22 '15 at 20:48
  • 1
    Ok, can you explain the problems with your code? You might want to read [How do I ask a good question](http://stackoverflow.com/help/how-to-ask), which enhances the probability for getting a useful answer _drastically_. You might find [ESR](https://en.m.wikipedia.org/wiki/Eric_S._Raymond)'s excellent essay [How To Ask Questions The Smart Way](http://catb.org/~esr/faqs/smart-questions.html) helpful, too. – Markus W Mahlberg Dec 23 '15 at 11:38
  • I am sorry, I was wondering are you familiar with golang ? Basically you can just copy paste the code and run it yourself . The question is self explanatory. I am not a writer !!! –  Dec 24 '15 at 09:14
  • 1
    You may not be, but that's not an excuse to clearly document your issue. What's happening on your side? We don't see what you're seeing, and we certainly don't want to spend a half an hour debugging a program just because you refuse to provide an acceptable explanation. Please remember that we are helping you for free, and you are expected to be nice and cooperative with everyone. Thanks! – Anonymous Penguin Jan 21 '16 at 02:05

2 Answers2

0

Well I see at least one problem a that is the case sensitive queries. So if your struct uses lowercase keys in json, you must use a lowercase one.

// This shoud match 
// against the "Name" property
selectedUser := bson.M{"Name": username}

updatedUser := bson.M{"$set": bson.M{
                "Name":      r.Form["username"][0],
                "Gender":    r.Form["gender"][0],
                "Age":       r.Form["age"][0],
                "CreatedAt": time.Now(),
 }}
RadekSohlich
  • 182
  • 1
  • 3
  • 11
0
data := model.Data {
            Name:      r.Form["username"][0],
            Gender:    r.Form["gender"][0],
            Age:       r.Form["age"][0],
            CreatedAt:  time.Now(),
        }

selectedUser := bson.M{"name": username}
updatedUser := bson.M{"$push": bson.M{"user": bson.M{"$each": []model.User{data}}}}
err = c.Update(selectedUser, updatedUser)

This will update the user array against matching username. It would be great if you can share the struct too. I have answered with the assumptions.