MONGOC_FIND_A...OPTS_SET_FLAGS(3)

✨ MONGOC_FIND_A...OPTS_SET_FLAGS(3) ✨

okay bestie, sit down, hydrate, and let's talk about libmongoc's mongoc_find_and_modify_opts_set_flags() β€” the little function that tells your find-and-modify builder what energy to bring πŸ’…


🌸 SYNOPSIS 🌸

bool
mongoc_find_and_modify_opts_set_flags (
   mongoc_find_and_modify_opts_t *opts,
   const mongoc_find_and_modify_flags_t flags);

πŸŽ€ PARAMETERS πŸŽ€

  • opts β€” a mongoc_find_and_modify_opts_t. the builder babe herself 🫢
  • flags β€” .
and yes, the official description for flags is literally just a period 😭 not me reading the source docs and finding a whole single dot. iykyk. moving on!!

πŸ’– DESCRIPTION πŸ’–

so what this bad boy does is add one or more flags to the builder. that's it, that's the vibe. here's your flag menu, no cap πŸ“

  • MONGOC_FIND_AND_MODIFY_NONE β€” the default. doesn't add anything to the builder. it's giving "i'm just here so i won't get fined" ✨
  • MONGOC_FIND_AND_MODIFY_REMOVE β€” will instruct find_and_modify to remove the matching document. bye bestie πŸ‘‹ literally deleting them from existence πŸ’€
  • MONGOC_FIND_AND_MODIFY_UPSERT β€” update the matching document or, if no document matches, insert the document. so if they don't exist yet, we simply make them exist. main character energy πŸ‘‘
  • MONGOC_FIND_AND_MODIFY_RETURN_NEW β€” return the resulting document. not the old one, the new one. the glow-up version 🌈

⭐ RETURNS ⭐

Returns Returns true if it successfully added the option to the builder, otherwise false and logs an error. (yes it really says "Returns Returns" twice β€” iconic of the docs honestly, we're keeping it, no edits πŸ’…)

so real talk: true = slay, false = it logged an error, go read it πŸ™ˆ


πŸ¦‹ SETTING FLAGS πŸ¦‹

here's flags.c, the whole vibe in code form:

void
fam_flags(mongoc_collection_t *collection)
{
   mongoc_find_and_modify_opts_t *opts;
   bson_t reply;
   bson_error_t error;
   bson_t query = BSON_INITIALIZER;
   bson_t *update;
   bool success;

   /* Find Zlatan Ibrahimovic, the striker */
   BSON_APPEND_UTF8(&query, "firstname", "Zlatan");
   BSON_APPEND_UTF8(&query, "lastname", "Ibrahimovic");
   BSON_APPEND_UTF8(&query, "profession", "Football player");
   BSON_APPEND_INT32(&query, "age", 34);
   BSON_APPEND_INT32(&query, "goals", (16 + 35 + 23 + 57 + 16 + 14 + 28 + 84) + (1 + 6 + 62));

   /* Add his football position */
   update = BCON_NEW("$set", "{", "position", BCON_UTF8("striker"), "}");

   opts = mongoc_find_and_modify_opts_new();

   mongoc_find_and_modify_opts_set_update(opts, update);

   /* Create the document if it didn't exist, and return the updated document */
   mongoc_find_and_modify_opts_set_flags(opts, MONGOC_FIND_AND_MODIFY_UPSERT | MONGOC_FIND_AND_MODIFY_RETURN_NEW);

   success = mongoc_collection_find_and_modify_with_opts(collection, &query, opts, &reply, &error);

   if (success) {
      char *str;

      str = bson_as_canonical_extended_json(&reply, NULL);
      printf("%s\n", str);
      bson_free(str);
   } else {
      fprintf(stderr, "Got error: \"%s\" on line %d\n", error.message, (int)(__LINE__));
   }

   bson_destroy(&reply);
   bson_destroy(update);
   bson_destroy(&query);
   mongoc_find_and_modify_opts_destroy(opts);
}

and the output is giving:

{
   "lastErrorObject" : {
      "updatedExisting" : false,
      "n" : 1,
      "upserted" : {"$oid" : "56562a99d13e6d86239c7b00"}
   },
                       "value" : {
                          "_id" : {"$oid" : "56562a99d13e6d86239c7b00"},
                          "age" : 34,
                          "firstname" : "Zlatan",
                          "goals" : 342,
                          "lastname" : "Ibrahimovic",
                          "profession" : "Football player",
                          "position" : "striker"
                       },
                                 "ok" : 1
}

look at that indentation situation. we don't fix it, we respect it 🧚 the upserted field ate, the ok : 1 is the cherry on top πŸ“ (and yes, that goals math adds up to 342, the code did NOT come to play)


πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc β€” the founders of the whole situation 🫢

2009-present, MongoDB, Inc.


and that's the page bestie!! version 2.5.6, dated Oct 09, 2026 β€” libmongoc forever, mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from