Skip to content

Instantly share code, notes, and snippets.

@dumebi
Last active June 15, 2020 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dumebi/202042fe4abae47d0fc683771595e097 to your computer and use it in GitHub Desktop.
Save dumebi/202042fe4abae47d0fc683771595e097 to your computer and use it in GitHub Desktop.
Trying to update a struct
type User struct {
mogo.DocumentModel `bson:",inline" coll:"users"`
Username string `json:"username" binding:"required"`
Email string `idx:"{email},unique" json:"email" binding:"required"`
Password string `json:"password" binding:"required"`
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
// CreatedAt *time.Time
// UpdatedAt *time.Time
Verified *time.Time `json:"_verified"`
}
func init() {
mogo.ModelRegistry.Register(User{})
}
// Activate service removes one user
func (userservice Userservice) Update(user *entity.User, data map[string]interface{}) (*entity.User, error) {
conn := db.GetConnection()
defer conn.Session.Close()
doc := mogo.NewDoc(user).(*(entity.User))
for k, v := range data {
fmt.Printf("%s is also %s", k, v)
// (doc)[k] = v
}
/*
the way mogo updates is
doc.Username = x
doc.Email = Y
but i want to be able to pass in an interface with key and value, so
doc.key = value
*/
err := mogo.Save(doc)
if vErr, ok := err.(*mogo.ValidationError); ok {
return nil, vErr
}
err = mogo.Save(doc)
return doc, nil
}
@chidiwilliams
Copy link

First, why though? Why do you want to pass in an interface map? Why not just fill in the struct values?

@dumebi
Copy link
Author

dumebi commented Jun 14, 2020

i want to be able to fill the values dynamically. like in a normal uodate

@dumebi
Copy link
Author

dumebi commented Jun 14, 2020

how would you run an update for this?

@chidiwilliams
Copy link

chidiwilliams commented Jun 14, 2020

You should have the concrete values themselves (what is the password, firstName, lastName, etc. value itself?). If you have it simply pass it into the function and create a document with the ID of the existing document but with updated values. I don't know how to use mogo but it should probably look like below.

(But also, I have to say... I strongly suggest you just use the Mongo default library (or globalsign/mgo, at least). I really don't think you need all these wrappers, and they aren't well documented or maintained. E.g. I tried to find how to set the document ID in mogo's docs but couldn't find anything.)

func (userservice Userservice) Update(user *entity.User, password, firstName, lastName string) (*entity.User, error) {
	doc := mogo.NewDoc(user).(*(entity.User))
        // set the ID somehow
        // doc.ID = id
        doc.password = password
        doc.firstName = firstName

	err := mogo.Save(doc)
        // ...
}

@dumebi
Copy link
Author

dumebi commented Jun 14, 2020

yeah, the reason i picked mogo is, it's the only one that allows for a mongoose like schema with document ref (like mngoose)
or do you know another?
also what i didnt want with this
doc.password = password
doc.firstName = firstName
is that i have to pass all the elements in the struct every time

@chidiwilliams
Copy link

Nah, I don't know another library for doing that.

Yeah, in the main libraries, you can pass in a map into the update function (see: https://www.mongodb.com/blog/post/quick-start-golang--mongodb--how-to-update-documents; bson.M is an alias for map[string]interface]), but even then I wouldn't recommend, except you really need to.

If you use the actual value and the same struct you use for insertion, you retain more control about what's going in and coming out of the DB.

@chidiwilliams
Copy link

It's actually possible that mogo's Update function also allows you to pass in a map[string]interface, but I can't tell from the docs.

@dumebi
Copy link
Author

dumebi commented Jun 15, 2020

yeah the docs is horrible. i was actually thinking of creating my own wrapper around it.
but abeg, i don tire

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment