[CDRIVER-2317] Overwrite Fields in BSON Created: 06/Oct/17  Updated: 27/Oct/23  Resolved: 07/Oct/17

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Victor Stewart Assignee: A. Jesse Jiryu Davis
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Very often I find myself needing to reinit a bson_t just to change 1 field. Lots of unnecessary operations! It would be great if fields could be overwritten. Maybe an explicit overwrite option, so there is no search overheard with append operations.



 Comments   
Comment by Victor Stewart [ 07/Oct/17 ]

forgive my ignorance, thank you!

Comment by A. Jesse Jiryu Davis [ 07/Oct/17 ]

If a field is of fixed size (int32, int64, bool, decimal128, or double) then you can overwrite it whenever you want:

if (bson_iter_init_find (&iter, &bson, "fieldname") && BSON_ITER_HOLDS_INT64 (&iter)) {
   bson_iter_overwrite_int64 (&iter, 12345);
} else {
   /* error handling or fallback logic */
}

You can keep a bson_iter_t struct pointing at the field you want to change - an iterator is valid for as long as the bson_t is valid.

Generated at Wed Feb 07 21:14:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.