go-bongo / bongo

Go ODM for MongoDB
MIT License
486 stars 40 forks source link

No Update() method? #41

Open william-bratches opened 5 years ago

william-bratches commented 5 years ago

Hello everyone,

I'm looking to do a partial update on a document (as described in the mongo docs) but it seems this ODM doesn't support it. The save() method will wholesale rewrite a document with a given object - if any fields are omitted, they will be removed.

Any reason why this was omitted? If there's not a blocking issue, I could possible contribute and add this functionality in a PR.

Regards,

William

jraede commented 5 years ago

Just given the nature of structs in go it’s basically impossible to know whether something was left intentionally blank or whether it should be ignored. I recommend using a different struct for a partial update, or, if the fields aren’t always the same, update using a map instead.

Sent via Mobile


From: William Bratches notifications@github.com Sent: Sunday, April 7, 2019 16:29 To: go-bongo/bongo Cc: Subscribed Subject: [go-bongo/bongo] No Update() method? (#41)

Hello everyone,

I'm looking to do a partial update on a document (see the mongo docshttps://docs.mongodb.com/manual/reference/method/db.collection.update/ but it seems this ODM doesn't support it. The save() method will wholesale rewrite a document with a given object - if any fields are omitted, they will be removed.

Any reason why this was omitted? If there's not a blocking issue, I could possible contribute and add this functionality in a PR.

Regards,

William

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/go-bongo/bongo/issues/41, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AEXD4vzYXgifjbBd3DPVblk7C8BiRpg1ks5velVGgaJpZM4cg-8F.

seantcanavan commented 2 years ago

If you need strict support for optional fields then they'll have to be pointers. This allows differentiation for instance with numbers where 0 is their default value but is also a valid value. Unlike, say, time.Time which defaults to the linux epoch and has a .IsZero() function to check.