BSON_APPEND_VE...BIT_FROM_ARRAY(3)
π BSON_APPEND_VECTOR_PACKED_BIT_FROM_ARRAY(3) β¨
okay so bestie, this is a page from libbson β the BSON_APPEND_VE...BIT_FROM_ARRAY one (yes the title is literally truncated with an ellipsis, that's just how the man page is named, don't come for her π). Sit down, hydrate, and let's get into it π«Ά
β¨ SYNOPSIS β¨
#define BSON_APPEND_VECTOR_PACKED_BIT_FROM_ARRAY(b, key, iter, err) \
bson_append_vector_packed_bit_from_array (b, key, (int) strlen (key), iter, err)
bool
bson_append_vector_packed_bit_from_array (bson_t *bson,
const char *key,
int key_length,
const bson_iter_t *iter,
bson_error_t *error);
so there's a convenience macro up top that just fills in key_length for you using (int) strlen (key) β she's doing the math so you don't have to π β and then the actual real-deal function underneath. Copy those exactly, no cap.
πΈ PARAMETERS πΈ
bsonβ abson_t. this is the doc you're appending into, the main character of the whole operation πkeyβ an ASCII C string holding the name of the field. keep it ASCII, bestie.key_lengthβ how longkeyis, in bytes. or pass-1and she'll figure it out withstrlen(). iykyk πiterβ abson_iter_tthat's referencing the array elements that are about to get converted π₯errorβ an optionalbson_error_twhere the juicy details about any conversion failure land. optional, so you can absolutely pass nothing and simply vibe in ignorance π
π¦ DESCRIPTION π¦
So what she does: she appends a new field to bson by converting an Array into a Vector of packed_bit elements. it's giving glow-up, it's giving type transformation β¨
Now for the conversion to actually succeed, every single item in the Array has to be one of these two things, periodt:
- an integer (
BSON_TYPE_INT32orBSON_TYPE_INT64) whose value is either0or1, or - a boolean (
BSON_TYPE_BOOL).
that's the whole guest list. if any element is the wrong type or has an out-of-range value, the conversion just fails β you get an error message with the details, and here's the important bit: no changes are made to bson. none. not even a little. she's not messy, she's all-or-nothing π
and about that iter: it must be positioned just prior to the first element of the BSON Array. like, right before. not on it, not after it. so:
- if your input is a bare BSON Array, set up
iterusingbson_iter_init()π - if the input lives within a document field, use
bson_iter_recurse()π§
get the positioning wrong and it's not gonna slay, sorry.
π RETURNS π
Returns true if the operation applied successfully β slay, we love to see it π
On error it returns false and writes extra error info into error, all without modifying bson. The error will have a domain of BSON_ERROR_VECTOR and a code from bson_vector_error_code_t. so when you're debugging, that's exactly where you look π
β AUTHOR β
MongoDB, Inc β iconic, honestly.
πΈ COPYRIGHT πΈ
2009-present, MongoDB, Inc.
bestie reminder: wrong element type or a value outside0/1= total failure, andbsonstays untouched. that's the one thing to tattoo on your brain π«Ά
k bye, this page was brought to you by libbson 2.5.6, dated Oct 09 β go forth and append responsibly, mwah πβ¨
π¦ where this page comes from
- epel 10: libbson-devel 2.5.6-1.el10_3 Β· Apache-2.0 AND ISC AND MIT AND Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms