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β amongoc_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 π«Ά
π COPYRIGHT π
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
- epel 10: mongo-c-driver-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