Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Document] We should have a "safe" method to update documents #57

Open
johnsudaar opened this issue Oct 9, 2018 · 3 comments
Open

[Document] We should have a "safe" method to update documents #57

johnsudaar opened this issue Oct 9, 2018 · 3 comments

Comments

@johnsudaar
Copy link

A method that will add the bson.M{"$set": query}

To prevent dumm errors like:

document.Update(ctx, Collection, bson.M{"field": "value"}, doc)

@EtienneM
Copy link
Member

We could just modify this condition:

doc.setUpdatedAt(now)
if _, ok := update["$set"]; ok {
update["$set"].(bson.M)["updated_at"] = now
}

Replace it with:

 if _, ok := update["$set"]; !ok { 
 	update["$set"] = update
} 
update["$set"].(bson.M)["updated_at"] = now 

?

@Soulou
Copy link
Member

Soulou commented Oct 10, 2018

Or we could just check that the keys of update are all starting with a $ character and returning an error if there is none.

As a result it won't break code using '$inc' for instance.

@johnsudaar
Copy link
Author

Or a method like:

document.Set(ctx, Collection, bson.M{"field": "value"}, doc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants