MONGOC_FIND_AN..._OPTS_SET_SORT(3)
π MONGOC_FIND_AND_MODIFY_OPTS_SET_SORT β libmongoc, bestie edition
okay so you're in the libmongoc docs and you clicked on mongoc_find_and_modify_opts_set_sort β that's the whole name, the header just truncates it because whoever laid out this manpage was being dramatic π. Anyway. This is the little function that tells your findAndModify builder which document to grab when your query matches, like, way too many of them. Let's get into it β¨
β¨ SYNOPSIS β¨
bool
mongoc_find_and_modify_opts_set_sort (mongoc_find_and_modify_opts_t *opts,
const bson_t *sort);
that's it, that's the signature, she's short and iconic π
πΈ PARAMETERS πΈ
optsβ amongoc_find_and_modify_opts_t. your builder object, your main character, the thing you're stuffing options into π«Άsortβ this is the one that decides which document the operation modifies if the query selects multiple documents. like,findAndModifyliterally modifies the first document in the sort order specified by this argument. so if your query is catching like ten docs, sort is what picks the winner π
π¦ DESCRIPTION π¦
it adds the sort argument to the builder. that's the assignment, no cap.
and here's the nice part: sort does not have to remain valid after you call this function. so you can yeet it, destroy it, whatever β the builder already took what it needed π
β RETURNS β
returns true if it successfully added the option to the builder, otherwise false. simple as that, no notes π
π SETTING SORT π
this is the example, from sort.c, and it's honestly a slay. read it, bestie:
void
fam_sort(mongoc_collection_t *collection)
{
mongoc_find_and_modify_opts_t *opts;
bson_t *update;
bson_t sort = BSON_INITIALIZER;
bson_t reply;
bson_error_t error;
bson_t query = BSON_INITIALIZER;
bool success;
/* Find all users with the lastname Ibrahimovic */
BSON_APPEND_UTF8(&query, "lastname", "Ibrahimovic");
/* Sort by age (descending) */
BSON_APPEND_INT32(&sort, "age", -1);
/* Bump his goal tally */
update = BCON_NEW("$set", "{", "oldest", BCON_BOOL(true), "}");
opts = mongoc_find_and_modify_opts_new();
mongoc_find_and_modify_opts_set_update(opts, update);
mongoc_find_and_modify_opts_set_sort(opts, &sort);
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(&sort);
bson_destroy(&query);
mongoc_find_and_modify_opts_destroy(opts);
}
π Outputs π
and this is what she prints, verbatim, weird indentation and all (we respect the original formatting here, it's part of the vibe):
{
"lastErrorObject" : {"updatedExisting" : true, "n" : 1},
"value" : {
"_id" : {"$oid" : "56562a99d13e6d86239c7b00"},
"age" : 35,
"firstname" : "Zlatan",
"goals" : 343,
"lastname" : "Ibrahimovic",
"profession" : "Football player",
"position" : "striker",
"author" : true
},
"ok" : 1
}
π AUTHOR π
MongoDB, Inc β credited exactly like that, we're not editing the receipts π§
π COPYRIGHT π
2009-present, MongoDB, Inc. β periodt.
ok that's the whole page!! every flag, every field, every line of that example made it through, promise π don't come for me if you skimmed the code block, go back and actually read it, iykyk π
and before you close the tab: this whole thing is from libmongoc, version 2.5.6, dated Oct 09, 202β¦ (the footer got cut off in the middle of the year, iconic behaviour honestly β that's genuinely how it shows up, I'm not gonna invent a digit for you πΈ). mwah, go write some queries bestie πβ¨π
π¦ 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