BSON_APPEND_CODE_WITH_SCOPE(3)

πŸ’… BSON_APPEND_CODE_WITH_SCOPE(3) πŸ’…

okay so bestie, welcome to libbson section 3 β€” this is the page for BSON_APPEND_CODE_WITH_SCOPE, and honestly? it's a little niche but it slaps. sit down, grab a strawberry drink πŸ“, and let's get into it.


✨ SYNOPSIS ✨

the whole thing in code form, no cap:

#define BSON_APPEND_CODE_WITH_SCOPE(b, key, val, scope) \
   bson_append_code_with_scope (b, key, (int) strlen (key), val, scope)

bool
bson_append_code_with_scope (bson_t *bson,
                             const char *key,
                             int key_length,
                             const char *javascript,
                             const bson_t *scope);

so yeah, there's a macro version that does the strlen() for you (iconic of her honestly) and the real function underneath doing all the actual work.


πŸŽ€ PARAMETERS πŸŽ€

  • bson β€” a bson_t. that's the document you're appending into, the main character of this whole situation πŸ‘‘
  • key β€” an ASCII C string with the name of the field. just the label, bestie.
  • key_length β€” how long key is, in bytes. or you can pass -1 and let it figure the length out with strlen(). lowkey the lazy-girl option and we respect it πŸ’…
  • javascript β€” a NULL-terminated UTF-8 encoded string holding the javascript fragment. yes, the actual code. UTF-8, don't play with me πŸ™ˆ
  • scope β€” optional bson_t containing the scope for javascript. optional!! so you can skip it and nobody's mad 🫢

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so bson_append_code_with_scope() behaves just like bson_append_code() β€” same energy, same vibe β€” except it also lets you hand over a scope for the javascript function, and that scope comes in the form of a bson document. it's giving "code but make it have context" ✨

here's the tea on how it branches:

  • if scope is NULL, it appends an element with BSON type "code"
  • otherwise, it appends an element with BSON type "code with scope"

that's it, that's the whole personality. one little NULL check and the type flips. main character decision-making fr fr.


πŸ”₯ RETURNS πŸ”₯

returns true if the operation was applied successfully. slay βœ…

but heads up bestie β€” the function will fail if appending javascript and scope grows bson larger than INT32_MAX. that's a real limit, not a drill πŸ’€


πŸ’œ AUTHOR πŸ’œ

MongoDB, Inc β€” the icons themselves πŸ‘‘


2009-present, MongoDB, Inc.


and that's the page!! 2.5.6, Oct 09, 2026 β€” go forth and append your code-with-scope, bestie, you're gonna do amazing πŸ§šπŸ’–

πŸ“¦ where this page comes from